Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery loop through possible elements to see if they exist?

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

664 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