简体   繁体   中英

Access the attribute in Javascript

I want to access the value of an attribute defined in the checkbox. The code of defining the checkbox is :

<input id="checkboxes" type="checkbox" name="item" class="row" statusid="10" value="175627">

This peice of code is inside a phtml file .

Now in my JS file,I want to access the attribute value for statusid ie 10. How can I achieve this ?

Please refer below image for clear understanding. 在此处输入图片说明

Thanks for reading.

Use .getAttribute() :

document.getElementById("checkboxes").getAttribute("statusid")

-- View Demo --

You can use

var input = document.getElementById("checkboxes");
var statusID = input.getAttribute("statusid");

to get the attribute.

If you want to get the value of the statusid attribute:

Using jQuery:
$("#checkboxes").attr('statusid');

Pure Javascript:
document.getElementById('checkboxes').getAttribute('statusid');

If you want to get the value of input element that statusid attribute has value of 10 (will return 175627 for your example):

Using jQuery:
$("input[statusid='10']").val(); 

Pure Javascript:
document.querySelector("input[statusid='10']").value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM