How do I change element value based on the data attribute?

Jon

I've tried using textContent and innerHTML, but haven't been able to change hello world to goodbye.

<div data-test='hello'>
hello world
</div>

var test = document.querySelectorAll("[data-test='hello']");
//test.textContent = "goodbye";
test.innerHTML = "goodbye";
Mathiasfc

Definition by documentation: The querySelectorAll() method returns all elements in the document that matches a specified CSS selector(s), as a static NodeList object.

The querySelectorAll method return an array, then you can change the first element as follow:

var test = document.querySelectorAll('[data-test="hello"]');
test[0].innerHTML = "goodbye";
<div data-test='hello'>
  hello world
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I change data-magnify-src attribute of HTML element with jquery?

jQuery how to find an element based on a data-attribute value?

How to find an element based on a two data-attribute value?

Change element name based on attribute value XSLT

How do i change an element based on cookie value set in jQuery without reloading the page?

How do I get the value of the $ (this) .children inner element attribute?

How do I find an element by attribute value that contains certain text?

OR i xpath such based on nth element and attribute value

How do I change the value of an attribute that has a namespace?

How do I change my classes private attribute value?

How can I change a column value based on length of element in a DataFrame

Angular: How do I change value based on key value

How do I change a data-tags attribute with an onclick event?

How do I change the data type of a calculated attribute in DML with PostgreSQL?

How do I change selected option based on different element in jQuery?

How can I change the input element name attribute value in a razor view model using a custom attribute in a model?

Using xmlstarlet, how do I change the value of an element

How do I change value in a pandas series based on condition

How do I change the color of an infobox in shinydashboard based on a string, not a value?

How do I change the value of a cell based on the position of a team in a table?

How do I transform an array to hold data based on a value in the data?

How do I change the value of an object's attribute that's a reference to another object's attribute?

How to find the element with an attribute and change the attribute value in javascript?

How to find an element with a data attribute with unknown value?

jQuery how to find an element based on a data-attribute value (v2)?

How do I target the correct data attribute and child element when dealing will multiple occurrences of the same parent element?

How to change value of property inside data attribute

How to change element visibility based on checkbox value?

How do I select element by attribute in jQuery?