Why is my jquery not displaying my div??

Hi, I write a couple of if statement to display error/success messages if their divs have any span tags inside of them:

    if ($("#error").has("span")) {
        $(this).addClass("display").removeClass("hide");
    }
    if ($("#success").has("span")) {
        $(this).addClass("display").removeClass("hide");
    }

Open in new window


But its not working. The divs still have their classes of .hide (display = none), and they are not getting the class .display (display = block).  Anyone know how to make this work??

The Sign Up tab:
http://auroriella.com/login.aspx#signup
FairyBusinessAsked:
Who is Participating?
 
Ovid BurkeConsultant InstructorCommented:
The 'if' condition is already implicit in the jQuery has() filter, so all you need is this:

$('#error').has('span').addClass('display').removeClass('hide');
$('#success').has('span').addClass('display').removeClass('hide');

Open in new window


However, it might be even more efficient (less code) to define only the class to hide the elements, then use this:

$('#error').has('span').show();
$('#success').has('span').show();

Open in new window

0
 
leakim971PluritechnicianCommented:
Try :

http://api.jquery.com/length/
http://api.jquery.com/size/
if ($("span","#error").length > 0) {
        $(this).addClass("display").removeClass("hide");
    }
    if ($("span","#error").size() > 0) {
        $(this).addClass("display").removeClass("hide");
    }

Open in new window

0
 
FairyBusinessAuthor Commented:
nope, its not working. I tried this:

    if ($("span", "#error").size() > 0) {
        $(this).addClass("display").removeClass("hide");
        alert("success");
    }
    if ($("span", "#error").length > 0) {
        $(this).addClass("display").removeClass("hide");
        alert("success");
    }
    if ($("span", "#success").size() > 0) {
        $(this).addClass("display").removeClass("hide");
        alert("success");
    }
    if ($("span", "#success").length > 0) {
        $(this).addClass("display").removeClass("hide");
        alert("success");
    }

Open in new window


Its alerting so its working on some level, but its not removing or adding the classes.

http://auroriella.com/login.aspx#signup

Any ideas??
0
 
FairyBusinessAuthor Commented:
Wow, the one with the least amount of code worked! lol
Thanks!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.