-12

I have an input like this:

<input id="xx" data-p="">

How can i change the value inside data-p using javascript?

nima
  • 27
  • 1
  • 4

3 Answers3

0

You can use querySelctor() to select input and setAttribute() to set value of data-p.

document.querySelector('input[data-p]').setAttribute('data-p', 'something')

document.querySelector('input[data-p]').setAttribute('data-p', 'something')
<input data-p="">

You can also use HTMLElement.dataset so it will be

document.querySelector('input[data-p]').dataset.p = 'something'

document.querySelector('input[data-p]').dataset.p = 'something'
<input data-p="">
Nenad Vracar
  • 111,264
  • 15
  • 131
  • 153
0

using below code you can do that.

document.getElementById("TextboxID").setAttribute('data-p', 'something')

or

 document.getElementById("TextboxID").attributes["data-p"] = "Value";
Darshak
  • 853
  • 7
  • 18
0

What you want to change is called Data Attributes.

To change it use .dataset.p = 'new val'

Justinas
  • 37,569
  • 4
  • 61
  • 88