Solved

jQuery loop through possible elements to see if they exist?

Posted on 2011-02-10
3
438 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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 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…

751 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