user1614862 user1614862 - 5 months ago 10x
Javascript Question

Updating data-value of input text box oninput not working

I want to set data-value of text box with the value of itself and the value of another input box. I have written simple javascript snippet to handle this at oninput attribute, but it is not updating the data-value attribute with the expected value.

<form onsubmit="return false" >
First name : <input type="text" name="txtFirstName" data-value="" oninput=" = txtFirstName.value +' '+ txtLastName.value"/> <br><br>
Last name : <input type="text" name="txtLastName" oninput=" = txtFirstName.value +' '+ txtLastName.value"/>



You need to use dataset.value instead of data-value. For more info about accessing data attribute value visit here.

<form onsubmit="return false">
  First name :
  <input type="text" name="txtFirstName" data-value="" oninput="txtFirstName.dataset.value = txtFirstName.value +' '+ txtLastName.value" />
  <br>Last name :
  <input type="text" name="txtLastName" oninput="txtFirstName.dataset.value = txtFirstName.value +' '+ txtLastName.value" />