Another .js (jquery) question.

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
I'm trying to use a variable in place of a selector.

Here's the code without the variable

$('.registerForm #firstName').on('focusout', function() {

I want to swap out .registerForm for a var called thisForm

and set thisForm = .registerForm

I tried several variations of what's below, but it's not doing anything. When I call the page with the code and check the source, it's not interpreting it. It's just producing the exact code I used.

$(+thisForm + '#firstName').on('focusout', function() {


Thoughts?
 

purbeast0

No Lifer
Sep 13, 2001
52,924
5,795
126
$(thisForm + ' #firstName') should work.

you don't need the + infront of it, and you also need a space before #firstName in the string, otherwise you are trying to do $('.registerForm#firstName') which would be an object with id firstName and class registerForm, not an object with id firstName down inside an object with class registerForm.

i think something like $(thisForm).find('#firstName') would also work.
 
Last edited:

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
I think you had left out a '

But you were right.

This is what worked

$(formName+' #firstName').on('focusout', function() {
 

purbeast0

No Lifer
Sep 13, 2001
52,924
5,795
126
yeah i left out the closing ', i just fixed it above just for future reference heh.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,282
3,903
75
Technical issue: IDs are supposed to be unique throughout a page. So #firstName should be sufficient on a proper page.
 

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
Technical issue: IDs are supposed to be unique throughout a page. So #firstName should be sufficient on a proper page.

The issue is I may have instances where a user, via jQuery, ends up having multiple forms loaded into the dom, but they aren't visible. The forms could potentially have the same ID's due to formatting.
 

purbeast0

No Lifer
Sep 13, 2001
52,924
5,795
126
The issue is I may have instances where a user, via jQuery, ends up having multiple forms loaded into the dom, but they aren't visible. The forms could potentially have the same ID's due to formatting.

and his point is, that is very bad design. if you have multiple items with the same id, it won't return all of them - it will just return 1 of them. so your code still won't work as you want it to.

demo - http://jsfiddle.net/dzXKs/1/

just use classes.
 

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
and his point is, that is very bad design. if you have multiple items with the same id, it won't return all of them - it will just return 1 of them. so your code still won't work as you want it to.

demo - http://jsfiddle.net/dzXKs/1/

just use classes.

Done.

Revisted the reason why I was using ID's anyway. At this point, didn't even apply anymore.

Also, to my understanding, in regards to CSS/browser rendering, is that classes are faster as well because ID's require more seeking? It's been a month or so since I read an article about it.
 

purbeast0

No Lifer
Sep 13, 2001
52,924
5,795
126
Done.

Revisted the reason why I was using ID's anyway. At this point, didn't even apply anymore.

Also, to my understanding, in regards to CSS/browser rendering, is that classes are faster as well because ID's require more seeking? It's been a month or so since I read an article about it.

not sure about that, and to be honest, it doesn't really make sense. my understanding is that once it finds the id specified, it stops traversing the dom since it found the id. with classes, it would have to traverse the whole dom because it would have to look for them everywhere.

but again, i'm not really sure and have never been concerned with that stuff because modern browsers are fast enough that i don't think it matters much. i've never ran into performance issues due to this stuff though. however, creating nice css is also an art where everything cascades perfectly.

also, with regards to css, stay away from !important as well. if you are using them all over the place, it just means your writing bad css.
 

Occ

Senior member
Nov 11, 2009
276
0
76
Modern browsers are nice and fast, but can still be brought to their knees if you try, and if you have to support something older like IE8 for enterprise you definitely have to pay attention.

From what I've read, getting Ids is faster because the function for that in the browser is usually pretty good, and the id is supposed to be unique on the page, whereas potentially any element could have the class.

One way to optimize class selectors is to do things like $('div.div-class') instead of just $('.div-class'), so instead of looking in all elements, you're just looking at div elements.
 

TechBoyJK

Lifer
Oct 17, 2002
16,701
60
91
Modern browsers are nice and fast, but can still be brought to their knees if you try, and if you have to support something older like IE8 for enterprise you definitely have to pay attention.

From what I've read, getting Ids is faster because the function for that in the browser is usually pretty good, and the id is supposed to be unique on the page, whereas potentially any element could have the class.

One way to optimize class selectors is to do things like $('div.div-class') instead of just $('.div-class'), so instead of looking in all elements, you're just looking at div elements.

so if you prefix the selector with the object type, it filters the search based on that object type? hmmm didn't know what.
 

purbeast0

No Lifer
Sep 13, 2001
52,924
5,795
126
so if you prefix the selector with the object type, it filters the search based on that object type? hmmm didn't know what.

you can go very deep too...

$(div.parent ul.subobject li.child a.lowest) and so on and so on.

there are a lot of options you can put in selectors too, like get the nth div or object inside of another, or ones that have specific attributes. the jquery selector is very powerful.
 

GregGreen

Golden Member
Dec 5, 2000
1,681
3
81
One way to optimize class selectors is to do things like $('div.div-class') instead of just $('.div-class'), so instead of looking in all elements, you're just looking at div elements.

Quick response -- shorter selectors are almost always faster.

Longer response -- there are two things wrong with this idea:
  1. On browsers > IE8, '.div-class' would use getElementsByClassName under the hood and 'div.div-class' would use querySelectorAll. getElementsByClassName is significantly faster querySelectorAll.
  2. On older browsers, jQuery/Sizzle/the browser's CSS parser read the selector from right to left so it first looks up 'div-class' using getElementsByClassName then filters out the divs using getElementsByTagName on the result getElementsByClassName returned.
 
Last edited:

xantub

Senior member
Feb 12, 2014
717
1
46
basically, if you use #whatever, you don't need any more selectors, it's the fastest way to find anything. Also, you should not under any circumstances have more than one element with the same ID in the page (unless they're in an iframe, which technically is another page) they are meant to be unique.
 

Occ

Senior member
Nov 11, 2009
276
0
76
Quick response -- shorter selectors are almost always faster.

Longer response -- there are two things wrong with this idea:
  1. On browsers > IE8, '.div-class' would use getElementsByClassName under the hood and 'div.div-class' would use querySelectorAll. getElementsByClassName is significantly faster querySelectorAll.
  2. On older browsers, jQuery/Sizzle/the browser's CSS parser read the selector from right to left so it first looks up 'div-class' using getElementsByClassName then filters out the divs using getElementsByTagName on the result getElementsByClassName returned.

Yes, you are correct; any modern browser like Chrome or Firefox, and I assume the new IE, have very fast getElementsByClassName methods, so that seems to usually be fastest. However, IE8 does not have the getElementsByClassName function, so I'm not sure point #2 is correct for that scenario.

I quickly ran an IE8 and Firefox comparison on the following test I googled: http://jsperf.com/selectors-perf/3

And found that in IE8, using element.classname scores 23000 vs 20000 with just .classname. However, Firefox 24 scores 43000 using element.classname and 67000 using just .classname. So... having to support IE8 is awful. :'(

It would be nice if the jquery team updated their selector optimization page, because it seems to imply that element.classname is faster.
http://learn.jquery.com/performance/optimize-selectors/
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |