Jquery val () vs this.value for dropdowns

The code below uses this.value to get the value of the form dropdowns. Usually I saw only .val (). Is below a cross browser acceptable (especially older versions of IE)? Thanks!

$(':input', '#all').each(function() { alert(this.value); }); 
+4
source share
2 answers

Yes, this is acceptable, more readable, and cheaper (faster) than calling $(this).val() .

Simply put, $(this) refers to a jQuery object, and this refers to a DOM element.

Frequently asked questions here touch it briefly (in the section "Know your DOM properties and functions")

You should use the usual "this" when you need to use your own DOM APIs, and $ (this) when you need jQuery help.

I also suggest reading the following:

$ (this) vs this in jQuery

jQuery: What is the difference between '$ (this)' and 'this'?

When to use Vanilla JavaScript vs. jQuery?

utilizing-the-awesome-power-of-jquery-to-access-properties-of-an-element

this deactivated

+5
source

This should work

For jQuery val you need $(this).val()

-1
source

Source: https://habr.com/ru/post/1439085/


All Articles