• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / How to Use jQuery to Change Button Text

How to Use jQuery to Change Button Text

November 25, 2021 Leave a Comment

To change button text using jQuery, the simplest way is to use the jQuery text() method:

$("button").text("Submit");

You can also use the jQuery html() method to change button text.

$("button").html("Submit");

Let’s say I have the following HTML form:

<form action="/action_page.php" method="get" id="form1">
<label for="firstname">First Name:</label>
<input type="text" id="fname" name="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lname" name="lastname">
</form>

<button type="submit" form="form1" value="Submit">Send</button>

If I want to change the button text inside from “Send” to “Submit”, I can use the jQuery text() method to do this with the following Javascript code.

$("button").text("Submit");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("button").text("Submit");

We can also use the jQuery html() method to change the content of the button.

$("button").html("Submit");

While the text() method is very simple, the html() method gives us the ability to insert html into our button, which gives us more options when styling the content. In this case, with buttons, using the html() method probably wouldn’t make sense.

Changing Button Text Using jQuery with a Click

To change button text using jQuery, we can combine the text() method with a click event.

Let’s say I have the following HTML form, and I want to give the user the ability to change the button text from “Send” to “Submit”.


<div id="click-me">Click here to change button text</div>
<form action="/action_page.php" method="get" id="form1">
<label for="firstname">First Name:</label>
<input type="text" id="fname" name="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lname" name="lastname">
</form>

<button type="submit" form="form1" value="Submit">Send</button>

We can utilize both the jQuery click() method and jQuery text() method to change the button text from “Send” to “Submit”.

Below is the Javascript code which will allow the user to be able to update the text in the submit button:

$("#click-me").click(function(){
    $("button").text("Submit");
}); 

The final code and output for this example of how to change the text of a submit button using the jQuery text() method and Javascript is below:

Code Output:

Click here to change button text




Full Code:

<div id="click-me">Click here to change button text</div>
<form action="/action_page.php" method="get" id="form1">
<label for="firstname">First Name:</label>
<input type="text" id="fname" name="firstname">
<label for="lastname">Last Name:</label>
<input type="text" id="lname" name="lastname">
</form>

<button type="submit" form="form1" value="Submit">Send</button>

<script>

$("#click-me").click(function(){
   $("button").text("Submit");
});

</script>

Hopefully this article has been useful for you to understand how to change button text with jQuery.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Get the Top Position of Element
  • 2.  Using jQuery to Empty the Contents of an HTML Element
  • 3.  jQuery fadeOut – Fading Out Element in HTML
  • 4.  Using jQuery to Get the Bottom Position of Element
  • 5.  How to Use jQuery to Check if an Element is Visible
  • 6.  jQuery slideDown – How to Slide Down and Show an Element Using jQuery
  • 7.  Get the Window Scroll Position Using jQuery
  • 8.  Using jQuery to Disable Input
  • 9.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 10.  Using jQuery to Replace HTML Inside Div

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy