Web Analytics Made Easy -
StatCounter Get information from within a span class - CodingForum

Announcement

Collapse
No announcement yet.

Get information from within a span class

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get information from within a span class

    I have select words that are within a <span class="word">example</span>. I need to make javascript pull the word "example" here so I can use that as a search term, and return the value which is linked to that word in an array. What would be the best way to do this?

    I got this far, but now I am stuck...

    Code:
    var arrElements = document.getElementsByTagName('span');
    var words = new Array();
    	for (i = 0; i < arrElements.length; i++) 
    	{
    		// Save the current element
    		oElement = arrElements[i];
    		if(oElement.className == "word")
    		{

  • #2
    oElement.firstChild.nodeValue == "example"

    Comment


    • #3
      OK, so I got it to cycle through all spans with this, but the problem is that it prints the contents of any span tag, and I only want the ones with the id="word"
      Code:
      var arrElements = document.getElementsByTagName('span');
        for (i=0;i<arrElements.length;i++)
        {
      	var a = arrElements[i].firstChild;
      	alert (a.nodeValue);
        }
      and I got it to print via a specific tag ID like this, which prints the first id="word" value, but I have no idea how to get it to the next. Please help!
      Code:
      alert(document.getElementById('word').firstChild.nodeValue);

      Comment


      • #4
        Well you kind of had the right idea with your first example. I think Beagle's code threw your off a little but its was just an example.
        Code:
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title></title>
        <script type="text/javascript">
        function getWords()
        {
        	var arrElements = document.getElementsByTagName('span');
        	var words = new Array();
        	var j = 0;
        	for(var i = 0; i < arrElements.length; i++)
        	{
        		if(arrElements[i].className == 'word')
        		{
        			words[j] = arrElements[i].firstChild.nodeValue;
        			alert(words[j]);
        			j++;
        		}
        	}
        }
        window.onload = getWords;
        </script>
        </head>
        <body>
        <div>
        <p>This is an <span class="word">example</span> of a <span class="word">script</span> that does what you <span class="word">want</span>.</p>
        </div>
        </body>
        </html>
        ||||If you are getting paid to do a job, don't ask for help on it!||||

        Comment


        • #5
          awesome, thank you very much! I got it!

          Comment

          Working...
          X