Bryan Bryan - 3 months ago 28
jQuery Question

Access text-input attributes with jquery

I have the following code:

$('.centering').click(function (e) {
$(".thVal").replaceWith(function () {
console.log(this);
return this.value;
});
$('.editFields').hide();
});


This gives med the following output:

<input class="thVal" width="2" type="text" modelbind="LicenseHolder_LegalPerson_Name" value="Test Oil AB">
5505094091(rad 256)
<input class="thVal" width="2" type="text" modelbind="LicenseHolder_LegalPerson.PrimaryAddress_StreetAddress" value="Testgatan 1">
5505094091
<input class="thVal" width="2" type="text" modelbind="LicenseHolder_LegalPerson_PrimaryAddress_ZipCode" value="50010">
5505094091(rad 256)
<input class="thVal" width="2" type="text" modelbind="x => x_LicenseHolder_LegalPerson_PrimaryAddress_HomeTown" value="Växjö Sverige">
5505094091(rad 256)
<input class="thVal" width="2" type="text" modelbind="" value="0730730037">


When I write
console.log(this.value)
, the values of the inputs are displayed. But I want to access the value of the modelbind-attribute, and when I write
console.log(this.modelbind)
, I just get undefined.

How can I access the value?

Answer

You can get the value using .attr()

console.log($(this).attr("modelbind"))

And it will be better if you use data-attribute