user1614862 user1614862 - 2 years ago 116
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"/>


Answer Source

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" />

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download