javascript - Get Checked Box Value for URL Append -


i have 2 scripts. 1 gets checked boxes value , other append <a> tag. both work separately need value of 'str' placed in url append script , can't seem work.

my checkbox looks like:

<script> var checked, checkedvalues = new array(); $(document).on('change', 'input[type=checkbox]', function(e) {  checked = $('input[type=checkbox]:checked'); checkedvalues = checked.map(function(i) { return $(this).val() }).get();  $('.aaa').html(checked.length + ' checked'); if (checked.length) {     var str = checkedvalues.join(); }); </script> 

my second script append url like:

<script> $('.nextquestion').attr('href', function() { this.href = this.href + '&answers=+str+';  }); </script> 

i trying place value of 'str' in url. can help?

may str not accessible inside function of second script,you might have make global variable

var str if (checked.length) {     str = checkedvalues.join(); });  $('.nextquestion').attr('href', function() {    //here first check if str has value    this.href = this.href + '&answers=+str+';  }); 

Comments