Malik6 Posted June 24, 2011 Share Posted June 24, 2011 I have instructions I want the user to read before they can get access to a link. Is it possible to place a check box there and make sure the user clicks the checkbox first then the link becomes enabled otherwise it is disabled for them to continue? I hope that was clear and that someone can help. Link to comment https://www.neowin.net/forum/topic/1007392-disable-a-link-unless-checkbox-is-checked/ Share on other sites More sharing options...
0 SummerWolf Posted June 24, 2011 Share Posted June 24, 2011 You can use a mixture of jQuery and JavaScript to do this. Based on what you have said, I'm going to assume that you have some kind of form than handles the data (having a link isn't very handy as a user can just bypass the validation and use the URL bar to navigate to it). The following is an example using jQuery that inserts the submit button element into a div that is ready for it. If the checkbox is then 'unticked', the submit button will be removed. <html> <head> <script type="text/javascript" src="jquery-1.5.min.js"></script></head> <body> <form name="my_form" action="some_page.html" method="post"> <input type="checkbox" name="thing" id="thing" /> <div id="accept"> </div> </form> <script type="text/javascript"> /* Waits for the checkbox to be ticked */ $('#thing').click(function() { /* Makes sure that the checkbox is going to be 'ticked' when clicked */ if($(this).attr('checked') == true) /* Adds the submit button into the DOM */ $('#accept').append('<input type="submit" value="Submit" id="accepted_submit">'); else /* If the button is unticked, the submit button is removed */ $('#accept').empty(); }); </script> </body> </html> If you have any text inputs within the form, you're going to want to prevent the use of the return key (as it can submit the form on any element); an example of which can be found here: http://www.bloggingdeveloper.com/post/Disable-Form-Submit-on-Enter-Key-Press.aspx. Hope that helps. Link to comment https://www.neowin.net/forum/topic/1007392-disable-a-link-unless-checkbox-is-checked/#findComment-594098194 Share on other sites More sharing options...
0 Malik6 Posted June 24, 2011 Author Share Posted June 24, 2011 Thanks Jag, I looked at what you provided but the link wasn't working that you gave. Also, the head section is trying to pull jquery-1.5.min.js but not sure where to get that js file? When I tried placing your code the submit button doesn't appear and I believe it's because the js file is missing. Can you provide that? I'd love to give this a try. Link to comment https://www.neowin.net/forum/topic/1007392-disable-a-link-unless-checkbox-is-checked/#findComment-594098898 Share on other sites More sharing options...
0 SummerWolf Posted June 24, 2011 Share Posted June 24, 2011 Ah yes of course. I'd forgotten to use jQuery's link. I have my own copy of the .js file as it makes things easier for me. Just use this line instead: <script src="https://code.jquery.com/jquery-latest.js"></script> Also, the link I provided contains a full-stop (period) at the end. It shouldn't be there (sorry for not noticing it!). To be honest, seeing as you are only displaying T&C's (or whatever) I'm going to guess that there aren't any text inputs that can be called into focus by the user. If there aren't, you can just forget about disabling the return key. I have tested the code myself and it works so let me know how you get on! Link to comment https://www.neowin.net/forum/topic/1007392-disable-a-link-unless-checkbox-is-checked/#findComment-594099486 Share on other sites More sharing options...
0 Malik6 Posted June 24, 2011 Author Share Posted June 24, 2011 So I tried placing the following in the head <script src="https://code.jquery.com/jquery-latest.js"></script> And this portion in the body: <form name="my_form" action="continue.html" method="post"> <input type="checkbox" name="thing" id="thing" /> I've read the instructions provided above. <div id="accept"> </div> </form> <script type="text/javascript"> /* Waits for the checkbox to be ticked */ $('#thing').click(function() { /* Makes sure that the checkbox is going to be 'ticked' when clicked */ if($(this).attr('checked') == true) /* Adds the submit button into the DOM */ $('#accept').append('<input type="submit" value="Submit" id="accepted_submit">'); else /* If the button is unticked, the submit button is removed */ $('#accept').empty(); }); </script> When I click the check box, the submit button does not appear. Am I doing something wrong? Does the js need to be local or is there something I'm missing? Link to comment https://www.neowin.net/forum/topic/1007392-disable-a-link-unless-checkbox-is-checked/#findComment-594099496 Share on other sites More sharing options...
0 SummerWolf Posted June 24, 2011 Share Posted June 24, 2011 I have just tested your code on my local machine and it works fine. The jQuery file doesn't need to be local. The only thing I can think of is something that may be conflicting with the global handlers that jQuery uses. I have come across this problem before when I'm including stuff like Lightbox (which uses the prototype.js library). Change the '$' signs to 'jQuery'. So the code will look like this: <form name="my_form" action="continue.html" method="post"> <input type="checkbox" name="thing" id="thing" /> I've read the instructions provided above. <div id="accept"> </div> </form> <script type="text/javascript"> /* Waits for the checkbox to be ticked */ jQuery('#thing').click(function() { /* Makes sure that the checkbox is going to be 'ticked' when clicked */ if(jQuery(this).attr('checked') == true) /* Adds the submit button into the DOM */ jQuery('#accept').append('<input type="submit" value="Submit" id="accepted_submit">'); else /* If the button is unticked, the submit button is removed */ jQuery('#accept').empty(); }); </script> If that doesn't work, could you please paste the entire HTML code you're using so I can get a better idea of where it's going wrong. I know that the jQuery isn't the problem as it works perfectly on my machine. EDIT: I have found your problem. I provided an incorrect jQuery link. Use this one instead: <script src="https://code.jquery.com/jquery-1.5.min.js"></script> Talk about running around in circles! I do apologise. I swear that doesn't usually happen ;) Link to comment https://www.neowin.net/forum/topic/1007392-disable-a-link-unless-checkbox-is-checked/#findComment-594099630 Share on other sites More sharing options...
0 Malik6 Posted June 24, 2011 Author Share Posted June 24, 2011 Sweet. Thank you so much for your help and quick response. If you need any design help or SEO assistance, do feel free to contact me. This i was a simple yet huge help. Link to comment https://www.neowin.net/forum/topic/1007392-disable-a-link-unless-checkbox-is-checked/#findComment-594099748 Share on other sites More sharing options...
0 Mathachew Veteran Posted June 24, 2011 Veteran Share Posted June 24, 2011 To add to the discussion, the reason why jQuery 1.6 would not work, but 1.5 does is because of a change introduced in 1.6. More detailed info can be found here: http://api.jquery.com/prop/ An annoying and seemingly improper change imho. Link to comment https://www.neowin.net/forum/topic/1007392-disable-a-link-unless-checkbox-is-checked/#findComment-594099764 Share on other sites More sharing options...
0 SummerWolf Posted June 24, 2011 Share Posted June 24, 2011 On 24/06/2011 at 21:11, Mathachew said: To add to the discussion, the reason why jQuery 1.6 would not work, but 1.5 does is because of a change introduced in 1.6. More detailed info can be found here: http://api.jquery.com/prop/ An annoying and seemingly improper change imho. Indeed, I've just read up on it. There's another thing to remember. Link to comment https://www.neowin.net/forum/topic/1007392-disable-a-link-unless-checkbox-is-checked/#findComment-594099838 Share on other sites More sharing options...
Question
Malik6
I have instructions I want the user to read before they can get access to a link. Is it possible to place a check box there and make sure the user clicks the checkbox first then the link becomes enabled otherwise it is disabled for them to continue?
I hope that was clear and that someone can help.
Link to comment
https://www.neowin.net/forum/topic/1007392-disable-a-link-unless-checkbox-is-checked/Share on other sites
8 answers to this question
Recommended Posts