Arun
Arun

Reputation: 1

change attribute of a button to submit

Please help me to find how to change the attribute of a button to submit using JQuery when button is pressed.

For example, when I press a button with value 'OK', it should change to type 'submit' and value become 'Save'

Upvotes: 0

Views: 8066

Answers (3)

Fernando Campos
Fernando Campos

Reputation: 131

from another very good answer in stackoverflow:

With javascript:

 document.getElementsById("OK")[0].type = "button";

With jQuery:

 $("input[id='OK']").prop("type", "button");

And you cant change types of button in Explorer 8 and bellow.

Regards!

Upvotes: 0

saidesh kilaru
saidesh kilaru

Reputation: 748

======================================================================

javascript:

  function btn_onclick(){

      var btn=document.getElementById(button_id');

      btn.setAttribute('type', 'submit');
      btn.setAttribute('value', 'save');

  }

======================================================================

Jquery:

 function btn_onclick(){

      $('#' + button_id).prop('type', 'submit');
      $('#' + button_id).html('Save');

   }

======================================================================

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382806

You can do like this:

$('button[value="OK"]').click(function(){
   $(this).attr({
     type:'submit',
     value: 'Save'
   });
});

However, that is not a good practice. You can either use a submit directly or create a new submit button instead of changing the type because browsers may not behave in the similar fashion you might expect.

Upvotes: 1

Related Questions