0

Am new with Ajax but I know that you can use getelementbyid to update any element on the page with that id. What I want to know is how do you target a particular element on the page if they all have the same id?

<li class="a">something</li><li class="a">something b</li>

How do I target the second li? since they both using the same id, i cannot use getelementbyid('a') since this will only update the first element with that id.

This is my ajax script

function loadurl(dest) {
var XMLHttpRequestObject = false; 
if (window.XMLHttpRequest) { 
        XMLHttpRequestObject = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { 
        XMLHttpRequestObject = new 
        ActiveXObject("Microsoft.XMLHttp"); 
} if(XMLHttpRequestObject) { 
        XMLHttpRequestObject.open("GET", dest); 
        XMLHttpRequestObject.onreadystatechange = function() { 

    if (XMLHttpRequestObject.readyState == 4 && 
        XMLHttpRequestObject.status == 200) { 
        document.getElementById("a").innerHTML = 
        XMLHttpRequestObject.responseText; 
        delete XMLHttpRequestObject; 
        XMLHttpRequestObject = null; 
        } 
    } 
    XMLHttpRequestObject.send(null); 
} 

}

and this is link

<a onclick="loadurl('page.php?p=1&id=2')">
2
  • 6
    You should not reuse id's. That is why you are now struggling. To group elements use class names. Id's uniquely identify a single DOM element.
    – BonyT
    Jun 25, 2011 at 16:03
  • As a pointer: the reason that getElementById() has a singular name, as opposed to, for example, the plural name of getElementsByClassName(), is because it applies to, and should find/manipulate, only one element. Jun 25, 2011 at 18:44

2 Answers 2

2

Don't repeat IDs, use class names instead. IDs are meant to be unique.

<li class="a">something</li><li class="a">something b</li>

You could then use something like jQuery then, to select them.

$(".a")
4
  • 1
    Or you could use document.getElementsByClassName('a'); and then a for (or other) loop to do the same thing. jQuery's not necessary for everything (as good as it really, really is...). Jun 25, 2011 at 18:42
  • @David of course it's not, but it exists, so I suggest using it because it improves readability and it's really simple to use.
    – bevacqua
    Jun 25, 2011 at 18:45
  • 1
    True, but I think the vanilla-JS approach would have been more applicable (with a library approach, jQuery, MooTools...) as an addenda, to show the differences and (perhaps) benefits of a library. Jun 25, 2011 at 18:47
  • i hear you guys and thanx for the tip! But can someone please adjust my script above to accommodate the fix? Thank you.
    – Jay
    Jun 29, 2011 at 14:45
0

Using the same id more than once in an HTML page is invalid HTML. If you had used class names instead:

<li class="a">something</li><li class="a">something b</li>

then you could have used document.getElementsByClassName() to get an array containing all the elements with that class.

To put your response in the second element, use something like this:

document.getElementsByClassName("a")[1].innerHTML = XMLHttpRequestObject.responseText;

or, to put your response in all of the elements with that class name, use something like this:

var els = document.getElementsByClassName("a");
for (var i = 0; i < els.length; ++i)
    els[i].innerHTML = XMLHttpRequestObject.responseText;
6
  • I changed the tag to class now! So how do I implement your technic to work with the code above?
    – Jay
    Jun 29, 2011 at 14:46
  • thanx G, but does that means I have to enter the number (1) manually??? what if the LI is the 20th or 8th on the list??? How do you detect this and make sure the script picks this up?
    – Jay
    Jun 29, 2011 at 23:04
  • This kind of begs the question, why would you want to? If you are trying to target a particular element, give it a unique id and use document.getElementById(). If you are trying to target several elements, give them a common class name and use the code to target all of them. Just what exactly are you trying to do?
    – Gaurav
    Jun 30, 2011 at 3:07
  • ok let me explain better. I have a bunch of items eg <li>some title</li><li>another title</li><li>yet another title</li> and lets say if I click on 'yet another title' it should change to read something else like "you've clicked on "yet another title" without page refresh!
    – Jay
    Jun 30, 2011 at 10:30
  • Just give each a different id. <li id="a1">some title</li><li id="a2">another title</li><li id="a3">yet another title</li>. Pass the id to your function: <a onclick="loadurl('page.php?p=1&id=2','a3')">. Then modify your function to accept it: function loadurl(dest, targetId). Then use the parameter to target the element: document.getElementById(targetId).innerHTML = XMLHttpRequestObject.responseText;
    – Gaurav
    Jun 30, 2011 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.