HELP, please, with my code

Discussion in 'JavaScript' started by mousename, Sep 13, 2009.

  1. #1
    Hi. i'm javascript beginner. My problem is that i can't evaluate where and how to inject </ br> tag, to get names of my cars looking like a list.
    Thanks in advance!

    The code is:

    function show_cars () {
    var div = document.getElementById('id')
    var cars = new Array();
    cars[0] = "Volvo";
    cars[1] = "Ferrari";
    cars[2] = "BMW";
    for(x in cars) {
    var txt = cars[x];
    var new_txt = document.createTextNode(txt);
    div.appendChild(new_txt);
    }
    }
     
    mousename, Sep 13, 2009 IP
  2. ohteddy

    ohteddy Member

    Messages:
    128
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #2
    Have a look at the innerHTML method. Incidentally your br tag is backwards. It should be <br />.
     
    ohteddy, Sep 13, 2009 IP
  3. Dregond Rahl

    Dregond Rahl Peon

    Messages:
    16
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <div id="cars"></div>
    
    <script type="text/javascript">
    
    function show_cars () {
    
    	var div = document.getElementById('cars')
    	var cars = new Array();
    
    	cars[0] = "Volvo";
    	cars[1] = "Ferrari";
    	cars[2] = "BMW";
    
    	for(x in cars)
    	{
    		var txt = cars[x];
    		
    		var new_txt = document.createTextNode(txt);
    		var new_br = document.createElement('br'); // Br Tag Created
    		
    		div.appendChild(new_txt);
    		div.appendChild(new_br); // Br tag Appended after Each text node
    	}
    }
    
    show_cars ();
    
    </script>
    Code (markup):
     
    Dregond Rahl, Sep 14, 2009 IP