Sylar Sylar - 1 year ago 135
jQuery Question

Get input value using htmlfor

What’s the best way to accomplish something like this:


<!-- inside a loop -->
<i htmlfor="wish">
<!-- name or icon -->
<input type="hidden" class="wish-cls" name="wish" value="<%= %>" />
<!-- end loop -->


//var id = document.getElementsByTagName('wish-cls');
console.log("Clicked: " +;

If I click the input, when not hidden, I get the value but how to achieve this using
? Is it possible?

Answer Source

You can use jQuery attribute selector and next() method to do this like following.


    var value = $(this).next(".wish-cls").val();
    console.log("Clicked: " + value);
<script src=""></script>
<i data-id="wish">Click</i>
<input type="hidden" class="wish-cls" name="wish" value="1000" />

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download