Skip to content Skip to sidebar Skip to footer

Using Custom Data-attributes In Html In Onchange Event

The code below gives me an error 'Fieldname not defined' when I am defining data-attributes and using them as parameters in a function. The update_person.php is updating a record i

Solution 1:

You can refer to a custom data attribute using the dataset property:

update_person(this.value,this.dataset.fieldname, this.dataset.key,this.dataset.keyvalue)

Ref:

Reading the values of these attributes out in JavaScript is also very simple. You could use getAttribute() with their full HTML name to read them, but the standard defines a simpler way: a DOMStringMap you can read out via a dataset property.

Or the alternative, and slightly longer format using getAttribute().

update_person(this.value,this.getAttribute('data-fieldname'), this.getAttribute('data-key'),this.getAttribute('data-value'))

Post a Comment for "Using Custom Data-attributes In Html In Onchange Event"