cj91rs
cj91rs

Reputation: 67

How to append a href link with a select tag value

I am attempting to change a href link based on two different select tags changed

So far I have got it to append with 1 select tag, however i cannot get it to work with the other. Any help or suggestions would be appreciated. Thanks

$('#to').on('change', function() {
    var value2 = $(this).val();
});

var base_href="/converter/";

$('#from').on('change', function() {
    var value=$(this).val();

    if (base_href=="")
       base_href = $("#cch-link").attr("href");

    $("#cch-link").attr("href",base_href+value+'-to-'+value2);
});

Upvotes: 1

Views: 154

Answers (1)

Ram
Ram

Reputation: 144689

You are defining the value2 variable in the context of the first handler, outside of that context the variable is undefined, you can define a global variable or:

var base_href = "/converter/";

function setHref() 
{
   $("#cch-link").attr('href', function() 
   {
       var to = $('#to').val();           
       var from = $('#from').val();

       return base_href + from + '-to-' + to;
   });
}

$('#to, #from').on('change', setHref); // .change();

Upvotes: 2

Related Questions