开发者

Dynamic id in jquery

$("#someId_"+someName).css(....);

This is what i am doing开发者_C百科 to get the div in jquery. This is working just fine except when 'someName' contains String with multiple tokens like "Mohammad Adil"

is there any other way of doing the same thing ?

thanks..


Don't do that. You cannot have spaces in your id.

Quoting the HTML5 spec(emphasis mine)

The id attribute specifies its element's unique identifier (ID). The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.


You can't have spaces in ID's so you would have to convert it to a dash or something that is valid. someName.Replace(" ","-") is the code to convert the space to a dash.

If this doesn't help then please explain some more. is someName a list of names or is it a single name that needs to be an ID?


You cannot have spaces in your IDs, but there is a way to select the div anyway.

$("[id='someId_"+someName+"']")

I suggest instead of spaces, you use something else, like an underscore.

<div id="someId_Mohammad_Adil"></div>

Then your $("#someId_"+someName) will work (as long as someName has the spaces converted to underscores: someName = someName.replace(' ', '_');)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜