1

I was passing a parameter from an onclick event this way...

<span id="btn1" onclick="DoIt('Var1', 'Var2', 'Var3');return false;">
  Go!
</span>

<script type="text/javascript">
function DoIt(Var1, Var2, Var3) {
  // some code...
}
</script>

and evertything was working great until I realized that the page is calling the DoSave() function on page load.

I realize I need to encapsulate the DoSave() function like this...

$('#btn1').click(function(event) {
  event.stopPropagation();
  // where DoIt() code will end up...
});

but I'm confused about how to pass and access the variables that were attached in the onclick event..

onclick="DoIt('Var1', 'Var2', 'Var3');return false;"

How can this be acheived?

1 Answer 1

3

Why not simple use data attributes?

<span id="btn1" data-var="var1" data-var2="var2" data-var3="var3">
  Go!
</span>

<script type="text/javascript">
$('#btn1').click(function(event) {
  value1 = $(this).data('var');
  value2 = $(this).data('var2');
  value2 = $(this).data('var3');
});
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

When I try to do an alert to show what I am passing, they each return 'undefined'.
@Beengie: Please update your question with complete code

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.