Wednesday, February 22, 2012

How to change submit button text/size/css/ on form submit with jquery?

In this article will show your how to set the parameter of the submit button when click the submit button.

1.change text use the attr/val method

.attr() Description: Get the value of an attribute for the first element in the set of matched elements.
.val() Description: Get the current value of the first element in the set of matched elements.

$("#submitId").attr("value","Submitting");

or

$("#submitId").val("Submitting");

2.change size use the height/width method

.height() Description: Get the current computed height for the first element in the set of matched elements.
.width() Description: Get the current computed width for the first element in the set of matched elements.
$("#submitId").height(30);

$("#submitId").width(150);

3.change css use the css method

.css() Description: Get the value of a style property for the first element in the set of matched elements.

$("#submitId").css("color","green");

$("#submitId").css("font-size","20");

Demo:

DEMO



Below is the source code:
<html>
<body>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#myForm").submit(function(){
        //clone the submit button use to reset
        $("#tempDiv").html($("#submitId").clone().removeAttr("style",""));
      
        //change the button text
        $("#submitId").attr("value","Submitting");
        //change the height
        $("#submitId").height(30);
        //change the width
        $("#submitId").width(150);
        //change css color
        $("#submitId").css("color","green");
        //change css font-size
        $("#submitId").css("font-size","20");
        alert($("#firstName").val());
        return false;//remove this line will submit request to server site.
    });
    $("input[type=reset]").click(function(){
        $("#submitId").replaceWith($("#tempDiv").html());
        $("#firstName").val("");
    });
});
</script>
 <form id="myForm" method="post" action="">
    <input type="text" name="firstName" id="firstName"/>
    <input id="submitId" type="submit" value="Submit Button"/>
    <input type="reset" value="Reset"/>
 </form>
 <div id="tempDiv" style="display:none;">
 </div>
</body>
</html>

Related Articles
jquery disable button

1 comment:

  1. He time of life is short ; to spend that shortness basely, it would be too long rs2 gold, It's great to be great, but it's greater to be human Runescape Gold, One meets its destiny on the road he takes to avoid it rs gold

    ReplyDelete