I want to pass some dynamic paramters to jquery onClick event. Before, I used this HTML and JavaScript:
<!-- HTML -->
<a id="link123" href="link1" onClick="javascript:func1(${param1},${param2});">123</a>
/* JavaScript */
func1(param1,param2) {
// do something
}
The parameters param1
and param2
e from backend code and are dynamic. How I get this is not important here.
After using jQuery, I have this HTML and JS:
<a href="link123">
<input type="hidden" id="param1" value="${param1}"/>
<input type="hidden" id="param2" value="${param2}"/>
$(document).ready(function() {
$("#link123").click(function() {
var param1 = $("#param1")[0].value;
var param1 = $("#param1")[0].value;
func1(param1,param2);
});
});
func1(param1,param2) {
//do something
}
function func1 is there as before.
I'm not happy with this solution (passing params as inline hidden values). What are other, better ways to pass dynamic parameters to jQuery, in situation like this?
I want to pass some dynamic paramters to jquery onClick event. Before, I used this HTML and JavaScript:
<!-- HTML -->
<a id="link123" href="link1" onClick="javascript:func1(${param1},${param2});">123</a>
/* JavaScript */
func1(param1,param2) {
// do something
}
The parameters param1
and param2
e from backend code and are dynamic. How I get this is not important here.
After using jQuery, I have this HTML and JS:
<a href="link123">
<input type="hidden" id="param1" value="${param1}"/>
<input type="hidden" id="param2" value="${param2}"/>
$(document).ready(function() {
$("#link123").click(function() {
var param1 = $("#param1")[0].value;
var param1 = $("#param1")[0].value;
func1(param1,param2);
});
});
func1(param1,param2) {
//do something
}
function func1 is there as before.
I'm not happy with this solution (passing params as inline hidden values). What are other, better ways to pass dynamic parameters to jQuery, in situation like this?
Share Improve this question edited Jan 18, 2012 at 11:23 newcoderintown asked Jan 18, 2012 at 10:55 newcoderintownnewcoderintown 3672 gold badges5 silver badges11 bronze badges 1- ${param1} is actually JSTL expression – newcoderintown Commented Jan 18, 2012 at 11:10
3 Answers
Reset to default 4Data attributes are advised for this purpose. Use:
<input data-param1="${param1}" />
And then:
$('input').click(function(){
$(this).attr('data-param1');
});
You could inline those parameters like you did before.
$("#link123").click(function() {
func1(${param1}, ${param2});
});
That is, your PHP (or whatever) generates javascript with already substituted param values.
I assume you are trying to pass variable values from backend scripting language to Javascript? One possible way is to output them directly to the javascript
<script language='JavaScript'>
//it's a good idea to have all those vars collected in a single object in order to not overpopulate the global scope
var scriptOutput = {
param1 : ${param1},
param2 : ${param1}
}
$(document).ready(function() {
$("#link123").click(function() {
func1(scriptOutput.param1,scriptOutput.param2);
});
});
</script>