0

Very basic question, but how can I add/populate a parameter to the onClick function of an HTML element?

<a onClick="someFunction()" id="testLink">Some function</a>

//PSEUDO CODE
var someFunctionParam = document.getElementById('testLink');
someFunctionParam.addParameter(someParam);

Obviously "addParameter" isn't a valid method in Javascript, can anyone guide on how to do so?

2 Answers 2

1

The best way in my opinion is to rewrite you JS to something like this:

<a onclick="aNewFunction()" id="testLink">Some function</a>


function aNewFunction() {
  // Invoke your existing function here;
  someFunction();

  //PSEUDO CODE
  var someFunctionParam = document.getElementById('testLink');
  someFunctionParam.addParameter(someParam);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Best way I would think of is

$(function () {
    var parameter='Joe Root';
    var functionName = $('#testLink').attr("onclick");
    $('#testLink').attr("onclick",
    functionName.replace('()', "('" + parameter + "')"));
    alert($('#testLink').attr("onclick"));
});

function someFunction(name) {
    alert("Hi " + name);
}

As, you can see, this works http://jsfiddle.net/hXpSN/1/

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.