Saurabh
Saurabh

Reputation: 2472

In IE7 Jquery Selector not working

I have a simple html like -

<div id="mainDiv">
 <form method="post">
   <input type="hidden" id="txtId" value="123">
 </form>
</div>

I am using following selector for accessing hidden field -

var txtVal = $('#mainDiv #txtID').val();
alert(txtVal);

which is working fine in FF and cheome but in IE7, alert is saying "undefined".

Upvotes: 0

Views: 383

Answers (4)

Alon
Alon

Reputation: 879

Just use:

    var txtVal = $('#mainDiv #txtId').val();

By the way, ID is unique, why not using only #txtId ?

    var txtVal = $('#txtId').val();

Upvotes: 0

Richard Dalton
Richard Dalton

Reputation: 35793

Probably because the ID of the element is txtId and you're using txtID (upper case D) in the selector.

Also, why are you using two selectors when just #txtId would be fine?

Upvotes: 1

Yes Barry
Yes Barry

Reputation: 9846

IE must be more strict about case. Change #txtID to #txtId (lowercase 'd')

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

Selectors are case sensitive:

var txtVal = $('#mainDiv #txtID').val();

should be:

var txtVal = $('#mainDiv #txtId').val();

since the input id="txtId". Notice the lowercase d.

Upvotes: 0

Related Questions