Solved

jQuery loop through possible elements to see if they exist?

Posted on 2011-02-10
3
434 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

747 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

16 Experts available now in Live!

Get 1:1 Help Now