0

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

html.erb

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

JavaScript

$(".wish").click(function(e){
  e.preventDefault();
  //var id = document.getElementsByTagName('wish-cls');
  console.log("Clicked: " + e.target.value);
});

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

dakab
  • 5,379
  • 9
  • 43
  • 67
Sylar
  • 11,422
  • 25
  • 93
  • 166

1 Answers1

0

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

$("[data-id=wish]").click(function(e){
    e.preventDefault();

    var value = $(this).next(".wish-cls").val();
    console.log("Clicked: " + value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<i data-id="wish">Click</i>
<input type="hidden" class="wish-cls" name="wish" value="1000" />
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
  • Hi. Would using `data-id` be shorter: http://stackoverflow.com/questions/5309926/how-to-get-the-data-id-attribute – Sylar Jul 23 '16 at 08:06