1

I have three buttons and i want such tha when i click on one button the link on an a tag changes

<a  class="payverlink" href="exbronzeregistrationform.php">Continue to registration</a>

<button class="goldpac">Choose Plan</button>
<button class="silverpac">Choose Plan</button>
<button class="bronzepac">Choose Plan</button>

I want such that when i click on one button, it changes the link at .payverlink

I have tried

function bronze()
    {
        $('.payverlink').href="exbronzeregistrationform.php";
    }
    function silver()
    {
        $('.payverlink').href="exsilverregistrationform.php";
    }

<button class="silverpac" onclick="silver()">Choose Plan</button>
<button class="bronzepac" onclick="bronze()">Choose Plan</button>

But this changes to bronze function onclick of any of the buttons. Please whats the issue.

2
  • 4
    You can use jQuery attr : api.jquery.com/attr Commented Jun 22, 2017 at 11:59
  • Tried that also $('.bronzepac').click(function () { $('.payverlink').setAttribute('href', 'exbronzeregistrationform.php'); }); $('.silverpac').click(function () { $('.payverlink').setAttribute('href', 'exsilverregistrationform.php'); }); .but thisalso had the same issue Commented Jun 22, 2017 at 12:00

6 Answers 6

1

You could set your javascript code to trigger the button click and avoid using the onclick into html

$(document).ready(function() {
   $("button").on('click', function(){

       if ($(this).hasClass('goldpac')) {
           window.location="http://..."; /* or exbronzeregistrationform.php for example */
       } else if ($(this).hasClass('silverpac')) {
           window.location="http://..."; /* or exbronzeregistrationform.php for example */
       } else if ($(this).hasClass('bronzepac')) {
           window.location="http://..."; /* or exbronzeregistrationform.php for example */
       }
   });
});

You could add one more line in each case changing the a tag, but ti wont make a huge difference in your actions as it isn't used as you click the buttons.

$("a.payverlink").attr("href", "http://...."); /* or exbronzeregistrationform.php for example */

So, you could just remove the 'href' as you contantly will change the url from js.

Sign up to request clarification or add additional context in comments.

Comments

1

Use attr or prop, attr stands for attribute and prop for property!

$(
  function(){
    $('#google').attr('href', 'https://duckduckgo.com/');
    //or use prop
    $('#duckduckgo').prop('href', 'https://bing.com')
  }
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS Learning</title>
</head>
<body>
<h1 class="header"></h1>
<a id="google" href="https://google.com/">google is down!</a>
<br>
<a id="duckduckgo" href="https://duckduckgo.com/">I'm slow...</a>
</body>
</html>

Comments

0

I suspect that both functions are failing, since there is no such property href on JQuery object.

Use this approach instead:

$('.payverlink').prop("href","exbronzeregistrationform.php");

Comments

0

Have you try .prop() method of jQuery hopefully it works .

function bronze()
{
    $('.payverlink').prop('href','exbronzeregistrationform.php');
}
function silver()
{
    $('.payverlink').prop('href','exsilverregistrationform.php');
}

Comments

0

You can use the .attr function:

function bronze(){
  changeLink('exbronzeregistrationform');
}
function silver(){
  changeLink('exsilverregistrationform.php');
}

function changeLink(url){
  $('.payverlink').attr('href',url);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a  class="payverlink" href="exbronzeregistrationform.php">Continue to registration</a>

<button class="silverpac" onclick="silver()">Choose Plan silverpac</button>
<button class="bronzepac" onclick="bronze()">Choose Plan bronzepac</button>

Comments

0

function bronze()
{
    $('.payverlink').attr("href","exbronzeregistrationform.php");
}
function silver()
{
    $('.payverlink').attr("href","exsilverregistrationform.php");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a  class="payverlink" href="exbronzeregistrationform.php">Continue to registration</a>

<button class="goldpac">Choose Plan</button>
<button class="silverpac" onclick="silver()">Choose Plan</button>
<button class="bronzepac" onclick="bronze()">Choose Plan</button>

You can try this. it will helps you. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.