Solved

jQuery loop through possible elements to see if they exist?

Posted on 2011-02-10
3
436 Views
Last Modified: 2012-05-11
I have some jQuery like this:

    $('#mydiv1').css('color', 'red');
    $('#mydiv2').css('color', 'blue');
    $('#mydiv3').css('color', 'green');
    $('#mydiv4').css('color', 'orange');
    $('#mydiv5').css('color', 'purple');

Open in new window

I think it should be this, so it only acts if the element exists:

    if ($('#mydiv1')) {.css('color', 'red')}
    if ($('#mydiv2')) {.css('color', 'blue')}
    if ($('#mydiv3')) {.css('color', 'green')}
    ...

Open in new window

Is there a better way to check if the elements exist, with this naming convention? Something like a 'for each #mydiv(n) ' ?
0
Comment
Question by:nbierm65
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 125 total points
ID: 34867344
Don't check anythin, jQuery try to get the objects using your selector and with the objects it found apply the function(s).
If no object if found with your selector(s) nothing happen.

Else if you want to really know if the objects exists you can use the length attribute :

if( $('#mydiv1').length == 0 ) { alert("there's no object with mydiv1 as id"); }

Open in new window

0
 

Author Comment

by:nbierm65
ID: 34867371
OK. and there's no extra strain/load time for trying and then refraining from acting on nonexistent elements ?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34867395
>OK. and there's no extra strain/load time for trying and then refraining from acting on nonexistent elements ?
Inside the box, I think the checking is done.

It seems some jQuery plugins generate error if the object don't exist...

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now