Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could make jQuery cannot get dom by its id? [duplicate]

When i use

alert(j(this).attr("id"));

I can see the pop up window shows up _denominations[3].id.denominationId

but when i call

alert(j('#_denominations[3].id.denominationId').attr("id"));

The pop up window shows me 'undefined', the actual html DOM is looks like this:

<div class="form-field">
    <select id="_denominations[3].id.denominationId" class="removableDenom" name="denominations[3].id.denominationId">
</div>

So what chance could possibly make undefined pop up happens

like image 995
Dreamer Avatar asked Jan 25 '26 22:01

Dreamer


2 Answers

Try escaping the periods and brackets in the ID value itself. So your value:

'#_denominations[3].id.denominationId'

Becomes

'#_denominations\\[3\\]\\.id\\.denominationId'

It's likely that jQuery is confusing these with classes and attribute selectors.

Example: http://jsfiddle.net/jonathansampson/ZUMna/

like image 194
Sampson Avatar answered Jan 27 '26 10:01

Sampson


you need to escape the special characters in ID..that is [,],..... since jquery takes [] as attribute selector and . as class selector..

try this

alert(j('#_denominations\\[3\\]\\.id\\.denominationId').attr("id"));
like image 26
bipen Avatar answered Jan 27 '26 11:01

bipen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!