Joseph Raphael Joseph Raphael - 6 months ago 10
jQuery Question

Return variable in an onclick function

I have a javascript code that returns a variable called

foo


I need to pass this variable as text to an onclick function



function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}

var foo = getQueryVariable("dv1");

<ul class="answer-list">
<li><a class="btn rollover" onclick="nextQuestion(6, 'http://www.example.net/iclk/redirect.php?apxcode=042004&id=eT9HmN9XD3xMgT8nKUj0KRjMIWuXeTj0KN2-0N&dv1=FOOHERE');"><span>Yes</span></a></li>
<li><a class="btn rollover" onclick="nextQuestion(6, 'http://www.example.net/iclk/redirect.php?apxcode=042004&id=eT9HmN9XD3xMgT8nKUj0KRjMIWuXeTj0KN2-0N&dv1=FOOHERE');"><span>No</span></a></li>
</ul>





I need to know how to pass the variable
foo
into the onclick function link in the place of FOOHERE.

how to do that in order to get the correct link ?

Answer

You can call another function with two parameters, and the another function will call nextQuestion

HTML:

<ul class="answer-list">
    <li><a class="btn rollover" onclick="someFunc(6, 'http://www.example.net/iclk/redirect.php?apxcode=042004&id=eT9HmN9XD3xMgT8nKUj0KRjMIWuXeTj0KN2-0N&dv1=');"><span>Yes</span></a></li>
    <li><a class="btn rollover" onclick="someFunc(6, 'http://www.example.net/iclk/redirect.php?apxcode=042004&id=eT9HmN9XD3xMgT8nKUj0KRjMIWuXeTj0KN2-0N&dv1=');"><span>No</span></a></li>
</ul>

Javascript:

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

function someFunc(number, link) {
    link = link + getQueryVariable("dv1");
    nextQuestion(number, link )
}