Search code examples
phpjavascriptsajax

Javascript multiple values on the same ID


I have a loop in php that echos out a <input type="hidden" id="lol" value=$id />

Every time the loops go through i get a new value in the hidden input field as you can understand.

Now, im trying to grab the value from each of these items and get grab it with Javascript and SAJAX. The javascript im using now works, But! It only grabs the first value (because the ID is the same on each input)

Javscript:

function Showbooking() {
    id = document.getElementById('lol').value;
    x_showBookingForm(id, do_showBookingForm);
}
function do_showBookingForm(html) {
    openPopup(600, 550, html);
}

As you can see im opening a POPUP with javascript aswell and exports the value into that popup window.

So on every popup I get the same value (the value from the first input).

How Do I get around this problem?


Solution

    1. Change ID to name
    2. Use document.getElementsByName and loop
    var lols = document.getElementsByName("lol");
    var vals=[];
    for (var i=0, n=lols.length;i<n;i++) {
      vals.push(lols[i].value);
    }
    alert(vals.join(","));