2

Suppose I have following code:

<div class="topPagination">
<select id="itemsPage" onClick="changeItems(this.value);">
<option value="0">12 Items per Page</option>
<option value="100">View All</option>
</select>
</div>
<div id="mainContent">
</div>
<div class="bottomPagination">
<select id="itemsPage" onClick="changeItems(this.value);">
<option value="0">12 Items per Page</option>
<option value="100">View All</option>
</select>
</div>
<script>
   function changeItems(itemsPage) {
         want to get the id of the current <select> box.
}
</script>

how to get the id of the current box. like if select top select box or if i select bottom box. Please help me to find a solution. I know with same ids is not the porper coding standard. But i have a situation like this.

4
  • You shouldn't duplicate ID's, ID's are supposed to be unique as they Identify a specific element. Commented May 10, 2015 at 16:08
  • Yes i know that. But i have a situation like this.<select id="itemsPage" onClick="changeItems(this.value);"> <option value="0">12 Items per Page</option> <option value="100">View All</option> </select>..this snippet of code is present in a jsp and i included it two times in same parent JSP. Commented May 10, 2015 at 16:12
  • Duplicating IDs will cause problems when using javascript. Commented May 10, 2015 at 16:13
  • Can you change from using ID's to a class? Commented May 10, 2015 at 16:20

4 Answers 4

1

This is not a valid HTLM code, all ids should be unique

But anyway you can use this code to get second checkbox

document.querySelectorAll('checkbox')[1];

if you need to browse through specific checkboxes, set them all some special class and use

document.querySelectorAll('.special_checkbox')[1];
Sign up to request clarification or add additional context in comments.

1 Comment

Checkbox? shouldn't this be select ? document.querySelectorAll('select')[1]
1

If you "want to get the id of the current box", it will always be "itemsPage".

Having 2 objects in the DOM with the same ID is not valid HTML.

However, if you can change the DOM but can't change the IDs for some reason, you could do something like this:

<select id="itemsPage" onClick="changeItems(this);">

and then make your function:

function changeItems(select) {
    // "select" is your element
    // "select.value" is your value (that used to be passed in as "itemsPage"
}

Ultimately you should try and change one of the IDs, or use classes instead.

Comments

1

Try utilizing HTMLElement.dataset to substitute duplicate ids ; data-id="itemsPage-1" for first id=itemsPage ; data-id="itemsPage-2" for secondid=itemsPage ; pass selected element dataset.id at onClick event onClick="changeItems(this.value, this.dataset.id);"

function changeItems(itemsPage, id) {
 console.log(itemsPage, id)
}
<div class="topPagination">
  <select data-id="itemsPage-1" onClick="changeItems(this.value, this.dataset.id);">
    <option value="0">12 Items per Page</option>
    <option value="100">View All</option>
  </select>
</div>
<div id="mainContent">
</div>
<div class="bottomPagination">
  <select data-id="itemsPage-2" onClick="changeItems(this.value, this.dataset.id);">
    <option value="0">12 Items per Page</option>
    <option value="100">View All</option>
  </select>
</div>

Comments

1

I think I know what it is you're trying to but this is a shot in the dark.

If you can change from using id to class this will work just fine.

window.onload=function(){
 var PageItems=document.getElementsByClassName('itemsPage');
	for(var i=0; i<PageItems.length; i++){
  //Set Event Listeners for each element using itemsPage class name
	PageItems[i].addEventListener('change',changeItems,false);
	}
}
function changeItems(){
  // New selected index
var Selected=this.selectedIndex;
var changeItems=document.getElementsByClassName('itemsPage');
	for(var i=0; i<changeItems.length; i++){
      //Change all select options to the new selected index.
		changeItems[i].selectedIndex=Selected;
	}
}
<div class="topPagination">
<select class="itemsPage">
<option value="0">12 Items per Page</option>
<option value="100">View All</option>
</select>
</div>
<div id="mainContent">
</div>
<div class="bottomPagination">
<select class="itemsPage">
<option value="07">12 Items per Page</option>
<option value="100">View All</option>
</select>
</div>

If you have any questions please leave a comment below and I will get back to you as soon as possible.

I hope this helps. Happy coding!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.