Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery loop through possible elements to see if they exist?

Posted on 2011-02-10
3
Medium Priority
?
447 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 500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
Suggested Courses

971 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