Manish
Manish

Reputation: 31

JQuery attribute starts with selectors issue

I am having an issue with JQuery attribute starts with selectors [name^="value"]. below is what i am trying to do

var parentContainer = $('#myparent');
$.each(parentContainer.find('*[name^="a[2].b[0].c"]'), function(){
 alert('Hi');               
});

Though the parentContainer have the elements with name starts with a[2].b[0].c still not able to alert 'Hi'.

But when i try to do it as mentioned below it works

var parentContainer = $('#myparent');
$.each(parentContainer.find('*[name^="a[2].b"]'), function(){
 alert('Hi');               
});

need ur help. Thanks in advance

Upvotes: 3

Views: 795

Answers (1)

James Allardice
James Allardice

Reputation: 165971

Have a look at the responses to this question.

The following quote could show the cause of your problem:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Square brackets are not valid characters for name (or id) attribute values, and while it will often still work, you may get unexpected results in some browsers.

This fiddle works fine for me in Firefox (only tried version 4), Safari 4 and 5, Chrome 12, IE8 and IE9 but fails (undefined is alerted) in IE6 and IE7.

Upvotes: 6

Related Questions