When you click an item, get the value of the hidden input of the child

In the following code, when I click on the anchor tag with the class des_searchDate I want to get the value of the immediate next value of the input field.

I tried to follow

<html>
    <div class="searched_date">
        <a href="#" class="des_searchDate">
            04-15-2014
            <input type="hidden" value="2014-04-15" name="searched-date">
        </a><br>
        <a href="#" class="des_searchDate">
            04-09-2014
            <input type="hidden" value="2014-04-09" name="searched-date">
        </a><br>
        <a href="#" class="des_searchDate">
            04-23-2014
            <input type="hidden" value="2014-04-23" name="searched-date">
        </a><br>
     </div>
 </html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<script type="text/javascript">

    jQuery(document).on('click', ".des_searchDate", function(){

    var decDate = jQuery(this).next().find('input').val();

alert(decDate);
});

</script>

But I get undefined in alert. What is wrong with my code?

+4
source share
5 answers
jQuery(document).on('click', ".des_searchDate", function(){
var decDate = jQuery(this).find('input').val();
alert(decDate);
});

demo fiddle

+3
source
jQuery(document).on('click', ".des_searchDate", function(){
    var decDate = jQuery(this).find('input').val();
    alert(decDate);
});

Here jsFiddle

http://jsfiddle.net/yT777/

+4
source

, :

jQuery(document).on('click', ".des_searchDate", function(){
var decDate = jQuery(this).find('input').val();
alert(decDate);
});

, :

jQuery(document).on('click', ".des_searchDate", function(){
var decDate = jQuery(this).nextAll('input:first').val();
alert(decDate);
});
+1
$('.des_searchDate').click( function(){
    var decDate = $(this).find('input').val();
    alert(decDate);
});
+1

javascript, jQuery:

Pure JS Fiddle

var sd = document.getElementsByClassName("des_searchDate");

for (i = 0;i<sd.length;i++){
  var s = sd[i];
  s.onclick = function(){
    var input = this.getElementsByTagName("input")[0];
    alert(input.value);
  }
}
0

Source: https://habr.com/ru/post/1537723/


All Articles