Gunnit
Gunnit

Reputation: 1074

How to replace a certain part of link on dropdown menue change?

i need some help with some javascript to replace a certain part of a link when i change the dropdown menue value. I would like to replace part of a link via javascript onchange of a dropdown menue. So when i change the dropdown menue i would like to change the number theTotal=12 in the link below with var e:

<a class="btn_full" href="/traveltour/index.php/paypal/buy?theTotal=12&amp;description=Pinko+Pallino+&amp;quantity=1&amp;tour_id=6&amp;tourextra_id=11&amp;guide_id=35">Go to payment</a>

 $(document).ready(function () {


        $("#Toursextra_count").on("change", function () {

            var a = 22;
            var b = $(this).find("option:selected").attr("value") * a;
            var c = $(this).find("option:selected").attr("value");
            var d = (b*0.10).toFixed(2);
            var e = parseFloat(d)+parseFloat(b);
            $(".totalcash ").text(b);
            $(".adults").text(c);
            $(".comission").text(d);
            $(".grantotal").text(e);
        });
    });

Upvotes: 0

Views: 46

Answers (1)

Wesley Smith
Wesley Smith

Reputation: 19571

You can use .replace() with a regular expression to manipulate the link's href attribute something like this:

jsFiddle

 $(document).ready(function () {
        $("#Toursextra_count").on("change", function () {

            var a = 22;
            var b = $(this).val() * a; 
            var c = $(this).val();
            var d = (b*0.10).toFixed(2);
            var e = parseFloat(d)+parseFloat(b);
            $(".totalcash ").text(b);
            $(".adults").text(c);
            $(".comission").text(d);
            $(".grantotal").text(e);
            var url = $('#myBtn').attr('href');
            var newSection = 'theTotal='+e+'&'
            url = url.replace(/theTotal=[\d.]+&/,newSection)
            $('#myBtn').attr('href',url);
        });
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<a class="btn_full" id="myBtn" href="/traveltour/index.php/paypal/buy?theTotal=12&amp;description=Pinko+Pallino+&amp;quantity=1&amp;tour_id=6&amp;tourextra_id=11&amp;guide_id=35">Go to payment</a>

<select name="" id="Toursextra_count">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>

Upvotes: 1

Related Questions