Categories
JavaScript Answers

How to click on element with text in Puppeteer and JavaScript?

To click on element with text in Puppeteer and JavaScript, we call the click method.

For instance, we write

const [button] = await page.$x("//button[contains(., 'Button text')]");
await button?.click();

to get the buttons with the ‘Button text’ text with the page.$x method.

We destructure the array to get the first one.

Then we call click on the first button to click on the button.

Categories
JavaScript Answers

How to delete all rows in an HTML table with JavaScript?

To delete all rows in an HTML table with JavaScript, we use the remove method.

For instance, we write

document.querySelectorAll("table tbody tr").forEach((e) => {
  e.remove();
});

to select all tr elements with querySelectorAll.

Then we call forEach with a callback to call remove to remove each tr element.

Categories
JavaScript Answers

How to add/update an attribute to an HTML element using JavaScript?

To add/update an attribute to an HTML element using JavaScript, we set the attributes property.

For instance, we write

const e = document.createElement("div");
e.attributes["id"] = "div1";

to create a div with createElement.

Then we set its id attribute by setting e.attributes["id"] to 'div1‘.

Categories
HTML

How to submit the value of a disabled input field with HTML?

To submit the value of a disabled input field with HTML, we add a hidden field with the name and value attribute values.

For instance, we write

<input type="text" value="22" disabled="disabled" />
<input type="hidden" name="lat" value="22" />

to add a disabled and hidden input with the same name and value attribute values so that the hidden value’s input is submitted.

Categories
JavaScript Answers

How to select element that does not have a specific class with JavaScript?

To select element that does not have a specific class with JavaScript, we use the :not pseudoclass.

For instance, we write

const el = document.querySelector("li:not(.completed):not(.selected)");

to select the first li element that doesn’t have the completed class or selected class with the querySelector method with :not