1

I have an HTML code with 2 types of messages when a Sign up DIV is clicked. When Sign Up error is clicked, the error message is displayed. When sign Up success is clicked, the success message is displayed. By default these messages are set to display:none.

My target output is to create a separate jQuery function "detectDisplay()" that executes "ONLY when success message has a CSS code display:block".

Below is my code. Any help is appreciated. Thanks.

CSS:

.cs_error { display: none; }
.cs_success { display: none; }

HTML:

<div class="cs_signUp" onclick="showError(); detectDisplay();">Sign up &raquo; error </div>
<div class="cs_signUp" onclick="showThanks(); detectDisplay();">Sign up &raquo; success </div>
<br/>
<div class="cs_success">
    <div>Thank you for signing up!</div>
</div>
<div class="cs_error">
    <div>Invalid input! Please try again!</div>
</div>

SCRIPT:

function showError() {
    $(".cs_success").css("display","none");
    $(".cs_error").css("display","block");
}

function showThanks() {
    $(".cs_error").css("display","none");
    $(".cs_success").css("display","block");
}

function detectDisplay() {

}
2
  • Since you want to fire the function only when the success message is displayed, you don't even have to check for the CSS display property. Just call the function within the showThanks() function will suffice, no? Commented Jan 8, 2016 at 6:54
  • Thanks for the comment Terry. As mentioned in my initial description, my target is to create a "separate" function. As in my implementation, the function showThanks() is somewhat hidden somewhere in the main script and untoucheable. I included showThanks() function to demonstrate in this mockup. Commented Jan 8, 2016 at 7:26

3 Answers 3

2

You can check the element if it is visible?

if($('.cs_success').is(':visible')){
  //do stuff
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I'll give it a try.
Thanks. This one works locally. Manjot's suggestion also worked. I am still implementing it in the production merging it with other codes.
2

you can use this to check if the element css is display block.

     <script type="text/javascript">
       if($('.cs_success').css('display') == 'block') { }
    </script>

Comments

1

Your detectDisplay() should be like following.

function detectDisplay() {
    if($('.cs_success').is(':visible')){
        //do your task here
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.