Search code examples
javascripthtmljqueryinternet-explorerjquery-events

Reading and changing form' action using jQuery in IE8


I have a jQuery code as follows:

function changeSomething(event) {
    var url = $(event.target).closest("form").attr("action");
}

My problem is $(event.target).closest("form").attr("action") is not reading the action attribute on IE (IE8) though it works perfectly on other browsers (i.e.: Chrome and Firefox). And I don't have any tags named "action" or "submit". So this problem can't be generated from it. Can anyone help me here?


Solution

  • Try to add this check to your code:

     var url;
     if (typeof event.target != 'undefined'){
      url = $(event.target).closest("form").attr("action");
     }
     else{
      url = $(event.srcElement).closest("form").attr("action");
     }
    

    I am not sure if event.target is supported by IE8

    Hope this helps!