Take the 2-minute tour ×

For the following HTML

<div id='parent'> 
  <input id='child' type=hidden value=''/>

I am doing

$('#parent #child').val('test')


$('#parent > #child').val('test')

but none of the above is working in IE7. It does work in Firefox though

Any idea why is it not working ?

share|improve this question
You could try using .attr("value") ? Not sure if that's the issue or not but perhaps worth a try? –  Jack Franklin Jun 30 '11 at 10:58
How do you know it is not working? What should happen? Setting the value with val will not change the source code. Btw, using $('#child') is easier and more correct. –  Felix Kling Jun 30 '11 at 10:58
I dont't know why it's not working but why do you do this? id is unique, so just use $('#child'). –  Karolis Jun 30 '11 at 10:58
Both should work. How do you know that's not working? How you inspect the value of the hidden field after calling your script? –  DanielB Jun 30 '11 at 11:01
add comment

3 Answers

is it because you've got the HTML wrong? You should use " for attribute values. Sometimes IE is more sensitive to these things than Firefox

share|improve this answer
add comment

Your syntax seems correct. The only stuff I can think of that can muck this up are:

  • Make sure you've got your code between a $(document).ready() block
  • Maybe try .prop() instead of .attr() if you're using jQuery 1.6+
share|improve this answer
add comment

Try $('#child').val('test'); which would probably give the same result.

share|improve this answer
add comment

Your Answer


