1

i have a list of items, each item is a button with a unique data-id like this

<button type="button" class="stuff" data-id="123">

i need to click on the button using the console

how would i select the button using only it's specific data-id (and Click on the button) using only javascript or jQuery, in the console?

the below answer is not a solution, because i cannot use the CSS query selector jQuery get an element by its data-id

3 Answers3

1

In jQuery, you would do it like so:

$("[data-id]") // all elements with data-id
$("button[data-id]") // all buttons with data-id
$("button[data-id]").click() // trigger click
Tammy Shipps
  • 865
  • 4
  • 13
1

You can select using query selector in javascript

document.querySelector('[data-id="123"]').click();

Jquery

$("[data-id='123']").click()
Viresh Mathad
  • 576
  • 1
  • 5
  • 19
0

Let's say the data-id you're targeting is:

const id = `sample`;

Then you can enter the above line followed by the following in the console to select it:

$(`[data-id="${id}"]`).click();
//OR
$('[data-id]').filter((i,b) => $(b).data('id') === id).click();
PeterKA
  • 24,158
  • 5
  • 26
  • 48