Change link name with jQuery

Asked

Viewed 131 times

1

I’ve done some research and I don’t know if this is possible.

In case what I am wanting to do is the following: With jQuery I would like to change the link name, like this:

It’s like this:

<a href="checkout-review" class="btn btn-primary" style="width:100%;">Escolha uma opção de Frete</a>

And after an event of onChange() go to:

<a href="checkout-review" class="btn btn-primary" style="width:100%;">Finalizar Pedido</a>

Notice that what I need to change is <a>AQUI</a>

How could I do this? Thanks in advance

  • @Leandrade did not solve my problem, I had seen this question before.

  • Maybe this link will help you https://www.w3schools.com/jquery/html_html.asp

  • Strange, because the answer is there, just do $("a").attr("href", "http://www.outra.coisa/");

  • @Leandrade in case the above example is changing href. And I need to change only the text. As answered below! But thank you for your attention. Thank you

  • Ta blaza, I got it wrong!

2 answers

2


Friend, you can put an id on the link and change the text property.
Below is an example:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    
</head>
<a href="checkout-review" id="link1" class="btn btn-primary" style="width:100%;">Escolha uma opção de Frete</a>
<button id="change" >Mudar texto</button>
<body>
    <script>
        $('#change').click(function(){
            $("#link1").text("Finalizar Pedido");
        });
    </script>
</body>
</html>

In your case you just won’t need to use the button to change, but then you can put in the event you want.

  • Thank you is just what I need. Thank you very much.

0

There are some ways to do this.

If you have control of link generation, then you can do it using a span:

<a href="checkout-review" class="btn btn-primary" style="width:100%;"><span>Escolha uma opção de Frete</span></a> 

$('a span').html( 'Finalizar Pedido' );

Now if you don’t have control of the generated link, then:

$("a").html($("a").html().replace("Escolha uma opção de Frete", "Finalizar Pedido"));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.