www.webdeveloper.com
Results 1 to 2 of 2

Thread: Run JS function on click

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Posts
    4

    Run JS function on click

    I've adapted a piece of code which copies fields over when a checkbox is checked. However I need this to work when a 'submit' button is clicked. I've added an event handler which runs the function once the button has been pressed. However it is not working, I'm not sure what's happening here. Here's my Fiddle http://jsfiddle.net/sui_page/z4wAr/

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,656
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title>123</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
    var done=false;
    $(document).ready(function(){
    $("#create-card").click(function(){
    if(!done){bindGroups();done=true;}
    else{unbindGroups();done=false;}
    return false;
    });
    });
    
    var bindGroups = function () {
        // First copy values
        $("input[name='b1']").val($("input[name='a1']").val());
        $("input[name='b2']").val($("input[name='a2']").val());
        $("input[name='b3']").val($("input[name='a3']").val());
    
        // Then bind fields
        $("input[name='a1']").keyup(function () {
            $("input[name='b1']").val($(this).val());
        });
        $("input[name='a2']").keyup(function () {
            $("input[name='b2']").val($(this).val());
        });
        $("input[name='a3']").keyup(function () {
            $("input[name='b3']").val($(this).val());
        });
    };
    
    var unbindGroups = function () {
        $("input[name='a1']").unbind("keyup");
        $("input[name='a2']").unbind("keyup");
        $("input[name='a3']").unbind("keyup");
    };
    </script>
    </head>
    <body>
    <form name="details">
        <input type="text" name="a1" />
        <br />
        <input type="text" name="a2" />
        <br />
        <input type="text" name="a3" />
        <br />
        <input id="create-card" type="submit" name="creat-card" value="enter" />
    </form>
            
    <form name="details-copy">
        <input type="text" name="b1" />
        <br />
        <input type="text" name="b2" />
        <br />
        <input type="text" name="b3" />
    </form>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles