0

Sorry - still a beginner - How do I dynamically create action url using the input text from the user?

 <form name="myInstitution"  action="" method="GET">
        <div>
            <label for="names" class="bold">Institution Name</label>
            <input id="names" name="schoolName" type="text"></input>                

        </div>

        <div class="bold"></div>

        <div>
            <input type="submit" value="Submit" id="sbutton" name="submit" />
            <input type="reset" value="Reset" />
        </div>
    </form>
user1769203
  • 351
  • 1
  • 9
  • 16

2 Answers2

1
$('#myForm').submit(function ()
{
    var action = '';
    // compute action here...
    $(this).attr('action', action);
});

Hope it will help you..

sasi
  • 4,192
  • 4
  • 28
  • 47
1

Since you don't seem to be using a javascript library, I'll copy the bind code in pure javascript from this answer.

var on = (function(){
    if ("addEventListener" in window) {
        return function(target, type, listener){
            target.addEventListener(type, listener, false);
        };
    }
    else {
        return function(object, sEvent, fpNotify){
            object.attachEvent("on" + sEvent, function(){
                fpNotify(window.event);
            });
        };
    }
}());


on(document.getElementById("sbutton"), "click", function(){
    this.action = document.getElementById( "names" ).val() + ".ext";
});

If you were using a javascript library like jQuery, you could bind to the event handler like this:

$( "form[name=\"myInstitution\"]" ).on( "submit", function( e ){
    var $this = $(this),
        text  = $this.find( "input#names" ).val();

    $this.prop( "action", text + ".ext" );
});
Community
  • 1
  • 1
rockerest
  • 10,412
  • 3
  • 37
  • 67