Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48187

Is using THIS more efficent than using the selector?

I have a simple form so user send his vote.
There I need to know what radio button user select.
The version I found to solve it was this. How can I get which radio is selected via jQuery?

value = $('input[name=vote]:checked', '#frmSurvey').val();

This work ok. Even when I dont understand how that work, because in Jquery selector documentation there is only 2 example with item separated by coma. And neither match my example where each element is inside a quote and then a coma

.class , .class ---> $(".intro,.demo") All elements with the class "intro" or "demo"
el1 , el2 , el3 ---> $("h1,div,p") All < h1>, < div> and < p> elements

Both looks like OR selector instead of find A and then find B inside A.

So if anyone can tell me what kind of selector is that I would love to take a look into the documentation

Now the optimization I was thinking. If I already inside a function for #frmSurvey won't be faster if I use the this element

 $('#frmSurvey').ajaxForm(function () {                                
            value = $('input[name=vote]:checked', '#frmSurvey').val();
            console.log('working way ' + value);

            value = $(this).find('input[name=vote]:checked').val();
            console.log('testing way ' + value);

But I couldn't make the second version to work. Second value get me undefined.

So how I fix second version?

And would be second version better than first one as my instinct suggest or I'm worrying too much?

Upvotes: 2

Views: 85

Answers (1)

Klors
Klors

Reputation: 2674

Your first example shows a selector operating from a context selector, whereas the documentation you've shown shows a "multiple selectors" selector.

You seem to have partially grasped this as

value = $('input[name=vote]:checked', '#frmSurvey').val();

is essentially the same as

value = $('#frmSurvey').find('input[name=vote]:checked').val();

However, the context of "this" inside your function is not clear as it depends upon how the ajaxForm plugin is coded. It isn't necessarily the result of your initial selector. After a short play with the plugin, it would appear that this in the context of ajaxForm is the jQuery ajax request object.

Upvotes: 1

Related Questions