简体   繁体   中英

How to dynamic select element with JQuery by id

I need to dynamic select element with JQuery, I get in code id of element. How to do that ? I've tried:

var sel='\'#'+id+'\'';
var elem+$(sel);

but it doesn't work ( id is string id of element).

You would use code such as

var ID = 'whatEver';
$('#' + ID).action();

You would then be able to use that to select whatever element you are after.

You don't need the extra quotes. Just:

var elem = $("#"+id);

A live example:: to find text area with attribute data-comment-id with any value

var value = "anyValue";
$('textarea[data-comment-id='+value+']')

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