Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here


Hide Multiple elements with same id using Attribute Equals Selector in jQuery

Share with WhatsApp

Hello friend, in this simple and short I am going to share the information about how you can hide or remove the multiple elements with same id in jQuery using Attribute equals selector.

As you may already know that one HTML document should not contain the multiple elements with same id and you should rather use a same class name for such requirement. But sometimes when markup is getting generated dynamically e.g. in MVC or Backbone then sometimes it is possible that you have multiple HTML elements with same ids.

So when you want to manupulate such element using jQuery, getting that object using ID selector returns only the first element and you end up with some UI related bugs assigned to you. :-)

Suppose there is a HTML code like below, in which we have 4 buttons with same id as "btnOK".

<button id="btnOK">button ok (1st)</button><br/>
<button id="btnOK">button ok (2nd)</button><br/>
<button id="btnOK">button ok (3rd)</button><br/>
<button id="btnOK">button ok (4th)</button><br/>
<button id="btnReload">Reload</button><br/><br/>
<button id="btnHideWithID">Hide buttons with ID selector</button><br/><br/>
<button id="btnHideWithAttr">Hide buttons with attribute selector</button>

and I want to hide the buttons with id as "btnOK", so for that I have written a simple jQuery code on click of "btnHideWithID" button as like below,


If you run this HTML, then you will find that clicking on button, only first "btnOK" gets hidden not all, this is because it is not valid mark up having multiple elements with same id due to which jQuery assumes you will have only one element with provided id so it hides only the first one.

But if you have the requirement in which you want to manupulate such multiple elements with same id then you should consider using Attribute Equals Selector like below.


It selects all the elements having "id" attribute value as "btnOK".

You can read more about Attribute Equals selector [name="value"] at this link.

Check out the live fiddle below.

Hope you have liked this simple post, If so, do not forget to share it with your tech friends! Thanks!

If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.

What is Hoisting in JavaScript?

Hoisting in JavaScript, one of the most misunderstood or little understood concept in the JavaScript and remain famous as interview question for JavaScript developers and now also for angular js developer. Lets check out what it means exactly.

Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.

How to post JSON string as a string in jQuery AJAX

Know how to post JSON String as string to the action method of controller in MVC.

How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.

Your opinion is valuable for us! Comments, suggetions are welcome.

Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!