Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show element with attribute

<input type="radio" name="group2" test="one" value="Water"> Water
<input type="radio" name="group2" test="two" value="Beer"> Beer<br>

<div style="display: none" test="one">aaaaaaa</div>
<div style="display: none" test="two">bbbbbbb</div>
<div style="display: none" test="one">ccccccc</div>

I would like: if i click on radio Water with attribute test="one" then should show me all div with attribute test="one". How can i make it with jQuery?

LIVE: http://jsfiddle.net/hRCXV/

like image 864
Gryz Oshea Avatar asked Aug 29 '26 22:08

Gryz Oshea


1 Answers

Try this:

$("input[type='radio']").click(function() {
    var test = $(this).attr("test");
    $("div[test]").hide();
    $("div[test='" + test + "']").show();
});

Updated fiddle

Please bear in mind that creating your own attributes as you have here is not valid. If you're using HTML5 you should consider using the data attribute to store whatever information you need associated with each element.

like image 174
Rory McCrossan Avatar answered Aug 31 '26 14:08

Rory McCrossan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!