How would I go about dynamically adding the <span> tags as innerHTML?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shelzmike
    New Member
    • Oct 2008
    • 11

    #1

    How would I go about dynamically adding the <span> tags as innerHTML?

    I have a issue, which I am thinking might not be that difficult.

    How would I go about dynamically adding the <span> tags as innerHTML (and consequently replacing what is currently in each cell) before the visibility changes.

    Seems to be I would first have to collect all of the nodeValues first, then loop through each cell and add an innerHTML of essentially this: '<span>' + nodeValues[i] + <'\span>

    Now, I am aware that this is not proper syntax, I am merely illustrating a point. But is this logical and possible?

    Mike
    Last edited by Niheel; Oct 13 '11, 03:45 AM. Reason: When posting new questions, best to start a new thread.
  • shelzmike
    New Member
    • Oct 2008
    • 11

    #2
    Answered my own question - it was actually pretty easy.

    Code:
    function hideRawData() {
    	var tbl = document.getElementById("users_table");
    	var tblRows = tbl.getElementsByTagName("tr");
    	var rawData = tbl.getElementsByTagName("td");
    	
    	for (k=0; k < rawData.length; k++){
    		//alert(rawData[k].firstChild.nodeValue);
    		rawData[k].innerHTML = "<span>" + rawData[k].firstChild.nodeValue + "</\span>";
    	}
    	
    	for(j = 0; j < rawData.length; j++) {
    		//rawData[j].style.display = "none";
    		rawData[j].firstChild.style.visibility = "hidden";
    	}
    	
       // alert ("Raw data has been hidden")		
    } // end hideRawData

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      note: HTML does not use backslashes, i.e. "</\span>" should be "</span>" (\s is a RegExp for whitespace)

      Comment

      • shelzmike
        New Member
        • Oct 2008
        • 11

        #4
        Yeah, I didn't mean to do that. Luckily it still worked, but I have since fixed it. Thanks!

        Mike

        Comment

        Working...