0
votes

I'm currently learning Javascript and ran into a small problem. The text between the Span tag should be displayed in the alert. I've tried using getElementby ID, Name, Class Name and TextContent. But without success. What I get instead of the name is "undefined" Can anyone help me out here? Thank you

Javascript:

function SayName(){

username = document.getElementById("nameof").textContent;
alert(username);}

HTML:

        <div class="swiper-slide card">
            <div class="card-content">
              <div class="image">
                <img src="images/Bake.jpeg" alt="">
              </div>
  
              <div class="media-icons">
                <i class="fab fa-facebook"></i>
                <i class="fab fa-twitter"></i>
                <i class="fab fa-github"></i>
              </div>
  
              <div class="name-profession">
                <span class="name" id="nameof">Lukas</span>
                <span class="profession">Back-End Developer</span>
              </div>
  
              <div class="rating">
                <i class="fas fa-star"></i>
                <i class="fas fa-star"></i>
                <i class="fas fa-star"></i>
                <i class="fas fa-star"></i>
                <i class="far fa-star"></i>
              </div>
  
              <div class="button">
                <button class="aboutMe" id="btn" onclick="SayName()">About Me</button>
                <button class="hireMe">Hire Me</button>
              </div>
            </div>
          </div>
1
Between the span tag, you mean both Lukas and Back-End Developer? It's not showing any error as it is - aca
I see alert with name "Lukas" !! - kiranvj
Maybe you didn't put the js file or the js code within script tags properly since there doesn't seem to be any apparent issues. - Indigo
Can't reproduce - jsfiddle.net/psjyn4r3 works fine. - CBroe
check if your js file is linked properly - Ankit Pandey

1 Answers

1
votes
Try this one:
var span_Text = document.getElementById("span_Id").innerText;