Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery selecting element by 'data-id' [duplicate]

Similar to the question here , I need to get an element based on its data-id. The problem I encounter is that I want to retrieve a an a element with a certain data-id using a variable.

lets consider the code below:

var item_id = 12345;

if I want to get the element that has the data-id with the value of item_id, would I do the following ?

$('[data-id=item_id]')

This does not work, and I am kind of stuck on how I can get around this.

like image 428
LogixMaster Avatar asked Jul 22 '26 15:07

LogixMaster


1 Answers

You can concatenate the variable using +:

$('[data-id=' + item_id + ']')
like image 108
Felix Avatar answered Jul 25 '26 04:07

Felix



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!