Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: multiple ids in getElementById?

  1. #1
    Join Date
    Aug 2005
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default multiple ids in getElementById?

    Can you have more than one id inside document.getElementById(), like document.getElementById(id1,id2) etc?

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by gusblake
    Can you have more than one id inside document.getElementById(), like document.getElementById(id1,id2) etc?
    No. The method only returns one element reference. This is indicated, in part[1], because of the singular usage of 'element'.

    Mike


    [1] The other, more telling indication is that function specification only lists one argument.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    With a little additional code you can grab more than one element, using getElementById(). But, what that additional code is depends upon what elements you want to grab and what you want to do with them. Here is one method:

    Code:
    els=['id1','id2','id3']
    for (var i_tem = 0; i_tem < els.length; i_tem++)
    document.getElementById(els[i_tem]).style.visibility='visible'
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript AHHH

    Hey guys, I am trying to dynamiccaly edit a text box depending upon what other fields have been entered.

    Sadly I cant post the entire code or a link due to it being an admin script but this is the JS code

    Code:
    function generateproddeschtml(){
    	<!-- ok generate the product description and place it into the description field -->
    	var NUMBEROF = document.getElementById('numberofrows').value;
    	THEFIELDS = Array();
    	for (var i = 0; i < NUMBEROF; i++){
    		THEFIELDS[i] = 'rowtitle_'+i;
    		alert(i);
    	}
    	var HTML = '<table width="460" cellpadding="0" cellspacing="0">';
    	for (var i_temp = 0; i_temp < THEFIELDS.length; i_teno++){
    		HTML = HTML + '<tr><td width="17"><img src="http://www.discsexpress.com/img/doughnut_red.gif" width="15" height="15" /></td><td height="21" width="200">'+document.getElementById(THEFIELDS[i_temp]).value+'</td><td width="250">'+document.getElementById('rowdesc_'+i)+'</td></tr>';
    	}
    	HTML = HTML + '</table>'
    	document.getElementById("description").value = HTML;
    }
    Code:
    and the error I get is
    Error: document.getElementById(THEFIELDS[i_temp]) has no properties
    Line: 84
    any ideas?

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There is so much messed up about that code that I cannot be certain of its actual intent. This could be what you want but, it depends upon the HTML of the page it is used on, so I have no idea if that fits. Also, unless the various fields exist and are filled out as expected, there will be problems even if everything else is set up correctly:

    Code:
    function generateproddeschtml(){
    //ok generate the product description and place it into the description field
    var NUMBEROF = document.getElementById('numberofrows').value*1;
    var HTML = '<table width="460" cellpadding="0" cellspacing="0">';
    for (var i_temp = 0; i_temp < NUMBEROF; i_temp++){
    HTML = HTML + '<tr><td width="17"><img src="http://www.discsexpress.com/img/doughnut_red.gif" width="15" height="15" /></td><td height="21" width="200">'+document.getElementById("rowtitle_"+i_temp).value+'</td><td width="250">'+document.getElementById("rowdesc_"+i).value+'</td></tr>';
    }
    HTML = HTML + '</table>'
    document.getElementById("description").innerHTML = HTML;
    }
    If document.getElementById("description") is a text field or text area, that last line should remain as it was:

    Code:
    document.getElementById("description").value = HTML;
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1
    There is so much messed up about that code that I cannot be certain of its actual intent. This could be what you want but, it depends upon the HTML of the page it is used on, so I have no idea if that fits. Also, unless the various fields exist and are filled out as expected, there will be problems even if everything else is set up correctly:

    Code:
    function generateproddeschtml(){
    //ok generate the product description and place it into the description field
    var NUMBEROF = document.getElementById('numberofrows').value*1;
    var HTML = '<table width="460" cellpadding="0" cellspacing="0">';
    for (var i_temp = 0; i_temp < NUMBEROF; i_temp++){
    HTML = HTML + '<tr><td width="17"><img src="http://www.discsexpress.com/img/doughnut_red.gif" width="15" height="15" /></td><td height="21" width="200">'+document.getElementById("rowtitle_"+i_temp).value+'</td><td width="250">'+document.getElementById("rowdesc_"+i).value+'</td></tr>';
    }
    HTML = HTML + '</table>'
    document.getElementById("description").innerHTML = HTML;
    }
    If document.getElementById("description") is a text field or text area, that last line should remain as it was:

    Code:
    document.getElementById("description").value = HTML;
    Hi there John,
    That looks a lot like my first code I added the array in due to the problem to see if that would cure it, i'll try the code in a few mins and see what happens

    Still getting the exact same error

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Then, most likely, the number entered in the:

    document.getElementById('numberofrows')

    field does not agree with the number of:

    document.getElementById("rowtitle_"+i_temp)

    fields and/or the number of:

    document.getElementById("rowdesc_"+i)

    fileds.

    Also, it would be impossible to get the same exact error, as the new code no longer references:

    document.getElementById(THEFIELDS[i_temp])

    There could be other problems because, as I mentioned before, without seeing the HTML of the page in question, I cannot know if this code fits with it or visa versa. You should create a demo page that contains all the elements relevant to this portion of code that you are trying to run and either post a link to it or include it as an attachment.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1
    Then, most likely, the number entered in the:

    document.getElementById('numberofrows')

    field does not agree with the number of:

    document.getElementById("rowtitle_"+i_temp)

    fields and/or the number of:

    document.getElementById("rowdesc_"+i)

    fileds.

    Also, it would be impossible to get the same exact error, as the new code no longer references:

    document.getElementById(THEFIELDS[i_temp])

    There could be other problems because, as I mentioned before, without seeing the HTML of the page in question, I cannot know if this code fits with it or visa versa. You should create a demo page that contains all the elements relevant to this portion of code that you are trying to run and either post a link to it or include it as an attachment.

    I just got a bad felling.... the rows start from 1 not 0 and the for loop starts at 0 which could be causing the error.... bah not enough time to many things ahhh thanks for the help john will have to squeeze it in

  9. #9
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey gusblake did you try the prototype $() function?? Well if you haven't then here's the link:

    http://www.dustindiaz.com/top-ten-javascript/

    Scroll down to the last function.

  10. #10
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shachi
    Hey gusblake did you try the prototype $() function??
    Why should anyone want to use anything from a library as badly-written as Prototype? Moreover, why are you addressing a post six months old?

    People unfamiliar with scripting would do well to avoid it. The rest would do better to write their own code.

    Mike

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •