开发者

Adding specific attribute to form item - jquery

开发者 https://www.devze.com 2023-04-04 18:24 出处:网络
Hi there men of the coding realm. Im trying to add the \'multiple\' attribute(?) to a select box via 开发者_如何转开发jquery.

Hi there men of the coding realm.

Im trying to add the 'multiple' attribute(?) to a select box via 开发者_如何转开发jquery.

essentially I want this:

<select tabindex="3" multiple="" style="width:350px;" title="Choose a Country">

but not sure how to add the 'multiple' via jquery?

Any ideas?

Cheers


When adding the attribute you need to give it a value. The actual value doesn't matter. The browser just looks for the presence of the attribute and doesn't take any notice of the value, but you can't just set it to an empty string. So just do:

$("select").attr("multiple", "multiple");

To remove the attribute, because you can't set it to an empty string, you use:

$("select").removeAttr("multiple");

Another similar example is the case of setting the disabled attribute on an input box.


Use attr method to add any attribute to dom element.

$("select").attr("multiple", "multiple");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号