简体   繁体   中英

How do I target “label for” with jQuery?

I'm trying to empty the HTML of this input and label via jQuery.

<input id="inputclass" type="checkbox">

<label for="labelclass">Text</label>

For the input it seems to be as simple as doing $(".inputclass").html(""); with jQuery, but what about for targeting the label?

jQuery selectors are basically CSS selectors, so:

$('label[for="labelclass"]').html('')

Also, <input> fields don't have an "html" property, so $('input').html('') has no effect.

First, an input can't contain HTML, it has a value , so the correct way to clear its value out would be:

$("#inputclass").val("");

Unless you want to deselect the checkbox, in which case it would be:

$("#inputclass").prop("checked", false);

A label, on the other hand, may contain nested HTML, so for it you can target the element type and then its for attribute value:

$("label[for='labelclass']").html("");

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