Change anchor text after click with jQuery


caustic

I am using the following code to show/hide a div when an anchor is clicked.

$(document).ready(function(){

    $(".slidingDiv").hide();
    $(".show_hide").show();

    $('.show_hide').click(function(){
        $(".slidingDiv").slideToggle();
    });

});

So when the "show_hide" div is clicked, the "slidingDiv" is displayed. However, once the "show_hide" div is clicked, how can I change it's text. For example, I want "show_hide" to show "Toggle Open", once I click "Toggle Close", how can I change it?

Thanks!

Brewer

You can do it like this:

demo

JS:

$('.show_hide').click(function(){
    $(".slidingDiv").slideToggle();
    $(this).toggleClass('open');
});

HTML:

<a href="#" class="show_hide">
    <span class="textopen">Toggle Open</span>
    <span class="textclose">Toggle Close</span>
</a>

CSS:

.textclose {display: none;}
.show_hide.open .textopen {display: none;}
.show_hide.open .textclose {display: inline;}

For example, I personally prefer to keep the text in html rather than JS for maintenance purposes. Keep your JavaScript clean too.

Related


Change anchor text after click with jQuery

caustic I am using the following code to show/hide a div when an anchor is clicked. $(document).ready(function(){ $(".slidingDiv").hide(); $(".show_hide").show(); $('.show_hide').click(function(){ $(".slidingDiv").slideToggle(); });

How to change anchor tag's text on click using jquery?

Start I want to change the text of anchor tag on click event using jquery. Anchor Tag: <a id="view_email" href="#"><i class="fa fa-envelope"></i> View Email</a> query code: <script type="text/javascript"> $(document).ready(function(){ $("#view_email").cl

jQuery change anchor link text

Charlie I have this anchor link: <li><a class="tab" id="watchTicket" href="#" onclick="CallPage('/section/tickets/view-ticket?action=watch&seq=<?php echo $_GET["seq"]; ?>');"><?php echo ($TicketWatched ? 'Un-Watch' : 'Watch'); ?></a></li> I want to be able to

Change button's text after click using jQuery

Aakanksha Choudhary I've been trying to change the text of a button, but it doesn't work in one of the fragments when clicked, but not in the other. Can someone help me understand what is missing in the code? So the first one works, but in the second code, the

Change button's text after click using jQuery

Aakanksha Choudhary I've been trying to change the text of a button, but it doesn't work in one of the fragments when clicked, but not in the other. Can someone help me understand what is missing in the code? So the first one works, but in the second code, the

Change button's text after click using jQuery

Aakanksha Choudhary I've been trying to change the text of a button, but it doesn't work in one of the fragments when clicked, but not in the other. Can someone help me understand what is missing in the code? So the first one works, but in the second code, the

jQuery change input button value attribute text after click

Mike Kaufman I have a problem with the jQuery code on my website. Website: LastDeath I want what the title says to change the value text in the input button (send button). I've googled and tested some code snippets from other questions, but this doesn't solve

Change anchor tag's text on click in ng-click method

username I have an anchor tag that when clicked should change its text in the ng-click function. Can someone please help me? Here is the code: <div ng-app="Test"> <div ng-controller="Foo"> <a class="anchClass" ng-click="hi($event)">click me</a> </div> an

Change anchor tag's text on click in ng-click method

username I have an anchor tag that when clicked should change its text in the ng-click function. Can someone please help me? Here is the code: <div ng-app="Test"> <div ng-controller="Foo"> <a class="anchClass" ng-click="hi($event)">click me</a> </div> an

jQuery change fixed element text color with anchor

username I'm trying to change the color of the text in a fixed header so that it scrolls to a specific div. I have little to no jQuery knowledge, but cobbled together a method, using the specified pixel measurement, that when scrolled to, the said text color c

jQuery change fixed element text color with anchor

username I'm trying to change the color of the text in a fixed header so that it scrolls to a specific div. I have little to no jQuery knowledge, but cobbled together a method, using the specified pixel measurement, that when scrolled to, the said text color c

jQuery - get text after closing anchor tag

Genesis Coin I have a table like this: <table border="1" width="40%"> <tbody> <tr class="child-row123" style="display: table-row;"> <td class="monsters">Monster</td> <td class="monsters"> <a data-name="Megalosmasher X" ta

How to change the text of <a> after click?

John Holliday I have this custom anchor button on my WordPress site: <a class="add-to-cart-button">Buy Now</a> I want it to change its text on click event. Is it possible to use javascript/php? Dylan After click, you can change innerHTML via JavaScript Using

Change link text after click

Christopher Orchard I am trying to change the text of a link when the user clicks the link. So far I have tried - <script src="/Includes/jquery-1.11.0.js"> $(document).ready(function() { $('[name="LT"]').click(function() { $('[name="LT"

How to change the text of <a> after click?

John Holliday I have this custom anchor button on my WordPress site: <a class="add-to-cart-button">Buy Now</a> I want it to change its text on click event. Is it possible to use javascript/php? Dylan After click, you can change innerHTML via JavaScript Using

Change link text after click

Christopher Orchard I am trying to change the text of a link when the user clicks the link. So far I have tried - <script src="/Includes/jquery-1.11.0.js"> $(document).ready(function() { $('[name="LT"]').click(function() { $('[name="LT"

How to change anchor tag text on click in angular 2

Prakash I have to toggle text On view and Hide how can I do this? I tried a lot but it didn't work. I don't want to use Jquery for this. I just want to use angular. Can anyone help me with this? ? <a class="history-link view-history-class" id="show-view-addre

Change text value on click with jQuery

Michael McGurk How can I change the text value from "closed" to "open" and vice versa when an H4 element is clicked? http://jsfiddle.net/8snk7ev6/ $(document).ready(function($) { $('#accordion').find('.accordion-toggle').click(function(){ //Expand

jQuery click to change .text value

Christopher I have a slider on my homepage that shows an image and some text. After a few seconds, the picture changes and so does the text. What I want to do is when someone clicks on the text, it goes to the corresponding page. For example: if the text is Ch

jQuery click to change .text value

Christopher I have a slider on my homepage that shows an image and some text. After a few seconds, the picture changes and so does the text. What I want to do is when someone clicks on the text, it goes to the corresponding page. For example: if the text is Ch

jQuery: Change button text on click

Shef After seeing this issue with the toggle button text in jquery , I tried recreating it for my situation, but it doesn't seem to work. Here is a fiddle of what I did : http://jsfiddle.net/V4u5X/ $('.SeeMore2').click(function(){ var $this = $(this);

jQuery - toggle text to change on click

the ball is hard I have a menu button that when clicked will slide down a div. My menu is titled "+Open Menu" When the menu button is clicked, my code changes the title to "+Menu" to indicate that the menu is open. When the user closes the menu I want the titl

jQuery - toggle text to change on click

the ball is hard I have a menu button that when clicked will slide down at the div below. My menu is titled "+Open Menu" When the menu button is clicked, my code changes the title to "+Menu" to indicate that the menu is open. When the user closes the menu I wa

Change text value on click with jQuery

Michael McGurk How can I change the text value from "closed" to "open" and vice versa when an H4 element is clicked? http://jsfiddle.net/8snk7ev6/ $(document).ready(function($) { $('#accordion').find('.accordion-toggle').click(function(){ //Expand

jQuery - toggle text to change on click

the ball is hard I have a menu button that when clicked will slide down at the div below. My menu is titled "+Open Menu" When the menu button is clicked, my code changes the title to "+Menu" to indicate that the menu is open. When the user closes the menu I wa

Replace anchor tag text after click without removing CSS

hmav25 Once I click it using jquery( replaceWith) I change the anchor tag text and it works fine. However, it also replaces the css of the anchor tag text. Can anyone help me how not to replace the css or add it again. My html is as follows: <div class="col-xs

Click on anchor tag with text

Tania How to click on anchor tag with specific text? For example, I have the following anchors in my page <a>one</a> <a>two</a> <a>three</a> Now, the user enters one. How to click a label with text as one? I tried this: var uservar='one'; $('a [text=$uservar]

Click on anchor tag with text

Tania How to click on anchor tag with specific text? For example, I have the following anchors in my page <a>one</a> <a>two</a> <a>three</a> Now, the user enters one. How to click a label with text as one? I tried this: var uservar='one'; $('a [text=$uservar]

Javascript: Change button text change after click

Valentine I have a simple question about changing the button text after the user clicks the button. I want the button click to toggle the text between two values: "more" and "less". Note that it must have the value "More" before the first click, and "Less" aft