Kim Kim - 4 months ago 7
HTML Question

Hide specific element based on value from name attribute

I have HTML like this:

<div>
<input name="studentName" value="George">
<input name="studentName" value="Kim">
<input name="studentName" value="David">
<input name="studentName" value="Kanto">
</div>


How can I hide other input element except "David" using jQuery?

Answer

You can use :not(), Attribute Equals Selector [name="value"], .hide()

$("div input:not([value=David])").hide()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
   <input name="studentName" value="George">
   <input name="studentName" value="Kim">
   <input name="studentName" value="David">
   <input name="studentName" value="Kanto">
</div>