简体   繁体   中英

Select Checkbox inside span using Javascript

I have a checkbox within a span container. How can I get the type and productid using Javascript?

Here's the innerHTML:

<span productid="6">
    <input id="MainContent_chk8" name="ctl00$MainContent$chk8" type="checkbox">
    <label for="MainContent_chk8">Test6</label>
</span>

I'd do something as simple as this:

var check = document.getElementById("MainContent_chk8"),
    productId = check.parentNode.getAttribute("productid");

Of course, this implies that you should know the id of the checkbox in advance - which may not be the case, as that seems to be a checkbox generated by the server.

It also implies that you are certain that the parent of the checkbox has the attribute you're looking for.

It may help if you quote more of your code. Your question is tagget "asp.net", so I guess this HTML snippet is generated by some ASP.NET code.

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