views:

637

answers:

7

Internet Explorer does not like my Jquery selector. Not sure if it's my weak Jquery skills or general Explorer strangeness.

Here is the Code:

$("#field_"+index+" #field_Label").val();

[div field_1]

<input id="field_Label" //... you get the picture.

to explain this.. I have a DIV labeled field_1, field_2.. etc.. Internet explorer appears to find the first iteration, but cannot find the second.

Thank you all, and thanks to you stackoverflow.

Is there a better way I should be doing this ?..

Here is a more complete snippet:

<li id="blank">
<div  id="field_1" style="background: #BDCFFF; color: #1028BD;margin: 10px; border: 1px solid black;width: 400px; height: 100px;">
<table>
<tr><td>Label:</td><td><input class="field_Label" id="field_Label" type="text"/></td></tr>
<tr><td>Type:</td><td><input id="field_Type" type="text" value="2"/></td></tr>
<tr><td>Id:</td><td><input id="idField" type="text" value="0"/></td></tr>
</table>

</div>
+2  A: 

Separate each selector with a comman:

$("#field_" + index + ", #field_Label")...........
Sarfraz
I don't think that's what he's after. My suspicion is that he is under the impression that "id" values are scoped under parent elements. That is not true. (Your answer isn't wrong, of course, but I don't think it applies.)
Pointy
@Pointy: not that sure either, but looking at his question this is what i could grasp. Thanks
Sarfraz
@Sarfraz well maybe it will be helpful to **somebody** at least :-)
Pointy
@Pointy: and i hope that **somebody** is questioner himself :)
Sarfraz
Yeah.. I was just trying to add commas.. didn't seem to have an effect.Strangely this works perfectly on Safari and Firefox..
A: 

you should give your div's a class and do

 var inputvals = $('.class_name').val();

then you can do what you want with them as a whole and you don't have to comma seperate a bunch of ids

mcgrailm
I was trying to break them apart since I'm pushing each of them onto an object in a Forloop.. I was trying to avoid that.. but might have to end up using .each on all of them individualy.
+3  A: 

Try using a class on the input instead of id. Only one input should have id field_Label.

<input class="field_Label" />
selector: $("#field_"+index+" .field_Label").val();

This is an update considering the code snippet you added. I rewrote it changing id to class.

<div  id="field_1">
<table>
<tr><td>Label:</td><td><input class="field_Label" type="text"/></td></tr>
<tr><td>Type:</td><td><input class="field_Type" type="text" value="2"/></td></tr>
<tr><td>Id:</td><td><input class="idField" type="text" value="0"/></td></tr>
</table>
</div>

Selectors:
var label = $("#field_1 .field_Label").val();
var type = $("#field_1 .field_Type").val();
var id = $("#field_1 .idField").val();

Class instead of id if you are going to have many tables with same kind of input. Otherwise if the inputs are unique just use id, example: selector: $("#idField").val()

Martin Larsson
thanks.. I'll check it out.
+1  A: 

Change #field_Label to .field_Label and then access

$("#field_"+index+".field_Label").val();

Without a space beween the values, so the output would be #field_1.field_Label - (Ie likes specifics)

jamie-wilson
Strangely.. I tried that.. changed the label to a class.. and backand forth.. still strange.alert ($("#field_1.field_Label").val());<input class="field_Label" id="field_Label" type="text"/>
I'm not sure if this is any help but you might want to drill down a bit, (#field_1, table, .field_label) or something, as tables can have funny effects on DOM structure in IE.Either way, if you just added the class name as class='field_1_Label' etc, you'd be able to access them directly and shouldn't be any harder doing it that way
jamie-wilson
+1  A: 

Thanks everyone for the help.. I did finally resolve this.. it was just strange since it only seemed to cause a problem with Internet Explorer.. but this is what I did to get it working.

$("#field_"+index).find("#field_Label").val();

/* Long live StackOverflow !! */

A: 

Thanks everyone for the help.. I did finally resolve this.. it was just strange since it only seemed to cause a problem with Internet Explorer.. but this is what I did to get it working.

$("#field_"+index).find("#field_Label").val();

/* Long live StackOverflow !! */

You saved me HOURS of time. Thank you!!! IE is the devil

syn4k
A: 
Thanks everyone for the help.. I did finally resolve this.. it was just

strange since it only seemed to cause a problem with Internet Explorer.. but this is what I did to get it working.

$("#field_"+index).find("#field_Label").val();

/* Long live StackOverflow !! */

As others have stated, the innermost .find() is not necessary and overly redundant.

Essentially, IDs are the fasted method of selection with jQuery's Sizzle selector engine. Using a unique ID, selection is the fastest method. Descending an ID from another ID only adds unnecessary additional computation. You most likely won't experience any performance slow-downs, but it's not a good habit to get into.

This is what you should be doing to access that element:

$("#field_label").val();

Source: http://www.artzstudio.com/2009/04/jquery-performance-rules/

Mattygabe