Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .attr() and value

I want to make the following use .attr();

selectbox.options[selectbox.selectedIndex].value

sadly,

selectbox.options[selectbox.selectedIndex].attr("value")

is not the same, and seems to defeat the purpose of the .attr altogether. My main question is: How should one use nested .attr()s?

like image 653
sova Avatar asked Feb 26 '26 19:02

sova


1 Answers

To get the value of any type of input element (including <textarea> and <select>) use .val():

var value = $(selectbox).val();

The .attr() translation would roughly be:

$(selectBox).find(":selected").attr("value");

....but just use .val() :)

The basic problem is that .attr() is a jQuery method. It's on jQuery objects, not on DOM elements directly, the same goes for almost all jQuery methods and plugins.

like image 188
Nick Craver Avatar answered Mar 01 '26 08:03

Nick Craver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!