Jquery find children element
I am trying to开发者_StackOverflow find the children element of a div my code looks like this , HTML
<div class="section">
<div id="element">
<input id="answer" type="hidden">
</div>
</div>
<input type="submit" id="submit" />
what i want is once i click submit it should find the class section and get me the value of hidden field , my js looks like this
$('#submit').click(function(){
answer = $('.section').children('#answer').val();
});
Thanks
You can use the find method instead.
$('.section').find('#answer').val();
Here's a demo: http://jsfiddle.net/jQh4q/
It's also worth pointing out that if you're using the ID "answer" in multiple places in your page, your html will not be valid.
I'm assuming this is the case since otherwise you'd be selecting the ID directly and not going in through its parent.
Each ID should be unique.
To retreive the value of #answer you can simply do this:
$('#answer').val();
$('#submit').click(function(){
var answer = $('#answer').val();
});
There is no need for children or such thing, IDs must be unique. So, you just select the element by its id directly.
精彩评论