SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pop alert on disabled submit button

    OK - so I have a submit button which is disabled unless you agree to the terms.

    What I want to do it pop an alert if the terms checkbox isn't checked.

    This is what I have so far, which enables the button is terms are checked, and disables if terms are unchecked... but how do I add an alert in there?

    Code:
    function apply()
    {
      if(document.form1.agree.checked==true)
      {
        document.getElementById("firstPageSubmit").disabled=false;
        
      }
      
      if(document.form1.agree.checked==false)
      {
       document.getElementById("firstPageSubmit").disabled=true;
     //  alert('You must agree with our terms and conditions before proceeding');
      }
     
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Tizer View Post
    This is what I have so far, which enables the button is terms are checked, and disables if terms are unchecked... but how do I add an alert in there?

    Code:
    function apply()
    {
      if(document.form1.agree.checked==true)
      {
        document.getElementById("firstPageSubmit").disabled=false;
        
      }
      
      if(document.form1.agree.checked==false)
      {
       document.getElementById("firstPageSubmit").disabled=true;
     //  alert('You must agree with our terms and conditions before proceeding');
      }
     
    }
    Is it as obvious as uncommenting the line that starts with "alert" ?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha ha - no - that is the code I was trying, and it wasn't working

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    If the firstPageSubmit button is not being disabled then it's a pretty good bet that the issue lies in how the function is being called.

    Can we see the html code for the form itself so that we can craft together some appropriate code to perform the job better for you.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The button is being disabled OK - but what I want to do is pop an alert box if the terms are not checked and the disabled button is clicked.

    I don't know javascript very well... but just writing that out has made me think that I am going about this all wrong... am I?

    Code:
    <form name="form1" method="post" target="_self" onSubmit="return ValidateForm()">
    
    <div class="future_check">
    <input type="checkbox" name="agree" onclick="apply()" />
    You must agree to our <a href="terms-and-conditions.html" target="_blank">Terms &amp; Conditions</a> before continuing
    </div>
    
    
    <div class="submitButton">
    <input type="submit" value="Register" name="firstPageSubmit" id="firstPageSubmit" disabled="disabled" />
    </div>
    </form>
    This is the code
    Last edited by Tizer; Dec 13, 2009 at 21:38. Reason: forgot code

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Tizer View Post
    The button is being disabled OK - but what I want to do is pop an alert box if the terms are not checked and the disabled button is clicked.
    Ahh, well that could be the problem then.

    Buttons that are disabled don't actually trigger the onclick event.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I thought when I wrote out my thread above...

    So how can I do something like this... is it in fact possible?

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,688
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Not with an actually disabled button.

    It is possible though to simulate the situation. Instead of having a disabled button, you can apply a class style to the button to make it look like it's disabled, with the onclick and onsubmit methods from the button performing similar checks and, when appropriate, returning false to prevent the action from occurring.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Addict Tizer's Avatar
    Join Date
    May 2002
    Location
    Beach
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - I'll give it a pop - thanks

  10. #10
    SitePoint Evangelist Dave Morton's Avatar
    Join Date
    Sep 2003
    Location
    Carson City, NV
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't really improve on Paul's answer, except to say good luck, and holler back if you need further help.
    Making a difference, one little psychotic episode at a time
    Geek Cave Creations
    Beta testers needed for pChat
    Dave's Gallery


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •