Skip navigation
Jibraan_786
Currently Being Moderated

Forms Help with checkboxes

Jul 4, 2012 12:14 AM

Tags: #help #dreamweaver #link #forms #buttons #checkbox

i want to click on a link that takes me to a form, but i also want a behavior that when i click on that link it will automatically check a "checkbox" in my form, its a common form with multiple options, please help

 
Replies
  • Currently Being Moderated
    Jul 4, 2012 4:33 AM   in reply to Jibraan_786

    Is the link on one page, and the form on a different page?  Are your pages just static HTML or are you able to use some server scripts on the page?  If so, which (PHP, NET, CF, etc.)?

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 4, 2012 5:48 AM   in reply to Jibraan_786

    You need to use JavaScript to do this. Give the checkbox that you want to be checked an ID. In the following example, I'm calling it "checkme".

     

    In the link, add a query string with the id like this:

     

    <a href="form.html?id=checkme">Go to form</a>.

     

    At the bottom of the page that contains the form, add the following <script> block just before the closing </body> tag:

     

    <script>

    function urlArgs() {

        var args = {},

            params = location.search.substring(1),

            pairs = params.split('&'),

            pos, name, value;

        for (var i = 0, len = pairs.length; i < len; i++) {

            pos = pairs[i].indexOf('=');

            if (pos == -1) continue;

            name = pairs[i].substring(0,pos);

            value = pairs[i].substring(pos+1);

            value = decodeURIComponent(value);

            args[name] = value;

        }

        return args;

    }

    function check(id) {

        var checkbox = document.getElementById(id),

            args = urlArgs();

        for (var name in args) {

            if (args[name] == id) {

                checkbox.checked = true;

                break;

            }

        }

    }

    check('checkme');

    </script>

     

    The only line that you need to alter is the last one:

     

    check('checkme');

     

    Replace 'checkme' with the ID of the checkbox.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points