0

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 ?

2
  • declare function nextQuestion(num, link){} Commented May 28, 2016 at 14:48
  • it's a long code and not related to the question I just need to know how to pass the var in place of FOOHERE... I need the syntax Commented May 28, 2016 at 14:54

2 Answers 2

1

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 )
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this simple code

function nextQuestion(num, link){
    var val = link.replace(/.*&dv1=/, "");
    console.log(val);
}
<a onclick="nextQuestion(6, 'http://www.example.net/iclk/redirect.php?apxcode=042004&id=eT9HmN9XD3xMgT8nKUj0KRjMIWuXeTj0KN2-0N&dv1=FOOHERE1');">Yes</a>
<a onclick="nextQuestion(6, 'http://www.example.net/iclk/redirect.php?apxcode=042004&id=eT9HmN9XD3xMgT8nKUj0KRjMIWuXeTj0KN2-0N&dv1=FOOHERE2');">No</a>

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.