1

I have a function to get a specific section of a URL and I save the results to a variable,

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 inputId = getQueryVariable("elementId");
alert(inputId);

The problem is that I can not add the variable inputId to my onClick event,

<a href="#" onClick="Javascript:parent.setValue('Test','inputId')">test</a>

Note that test is a dummy string so nothing to worry about.

Grady D
  • 1,889
  • 6
  • 30
  • 61
  • Please use the [formatting sandbox](http://meta.stackexchange.com/questions/3122/formatting-sandbox) for testing. – Cilan Sep 18 '14 at 21:24
  • @Shivam I think he wanted to add JavaScript to a post on SE – Cilan Sep 18 '14 at 21:25
  • 3
    Where is your click event? Why can't you call `getQueryVariable` during the click event? Have you tried to add it when you bind the click event? When is `getQueryVariable` called? – scrappedcola Sep 18 '14 at 21:26
  • would you mind using JQuery to do what you want? – Simon Paquet Sep 18 '14 at 21:28
  • My posted was edited and the click event was removed. I have added it back. I am perfectly fine using jQuery. I will have X number of results on page and every single result will need to have the variable `inputId` so I would like it to be efficient. – Grady D Sep 18 '14 at 21:29
  • Bind your click event elsewhere don't do it inline. Also `onClick` doesn't use format `Javascript:`. It already is javascript. What is parent and do you realize when you put `inputId` in single-quotes you make a string of "inputId" and not use the value of `inputId`? – scrappedcola Sep 18 '14 at 21:31
  • see http://stackoverflow.com/questions/2966160/how-do-i-bind-a-click-to-an-anchor-without-a-framework-javascript for how to bind events using javascript – scrappedcola Sep 18 '14 at 21:32

1 Answers1

2

Assuming inputId is a global variable, remove the single quotes from it in the onclick handler:

<a href="#" onClick="parent.setValue('Test',inputId)">test</a>

You can also exclude the word Javascript.

Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79