2

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.

Community
  • 1
  • 1
LogixMaster
  • 586
  • 2
  • 11
  • 36

1 Answers1

12

You can concatenate the variable using +:

$('[data-id=' + item_id + ']')
Felix
  • 37,892
  • 8
  • 43
  • 55