简体   繁体   中英

How can I use the attribute selector with a variable?

I need to use this form because it can handle spaces in my element ID.

$("div[id='content Module']").whatever();

However I need 'content Module' replaced with a variable?

I tried

$("div[id=" + my_var + "']").whatever();

but this did not work

You're missing a '

$("div[id=" + my_var + "']").whatever();

Should be

$("div[id='" + my_var + "']").whatever();

try this $("div[id='" + my_var + "']").whatever(); you forgot the open Apostrof

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM