javascript - How do I pass parameters into a jQuery.click() function on a <span> tag? -
i have used jquery.click() function in past buttons , have had nothing success, tried use click on <span>
, doesn't work.
also noticed automatically executed function without listening click.
judging how used on buttons, syntax:
<p><span id="example">click here</span></p> $("#example").click(examplefunction(p1, p2));
but not seem work. again executes without click taking place. tried:
$(document).on("click", "#example", examplefunction(p1, p2));
still no luck, same results.
i making weather app , goal toggle temperature between fahrenheit , celsius clicking on unit. made copy of code app on codepen.io here:
i appreciate help!
looks should try this:
$(document).on("click", "#example", function() { console.log('hello world'); });
using function()
definition alone give me uncaught syntaxerror: unexpected token (
.
what you're doing binding anonymous function click. if doing differently, myfunction()
, execute function.
if had myfunction
still trigger using click so:
function myfunction() { console.log('hurra!') } $('#example').click(myfunction)
notice didn't use parentheses otherwise run function instead of binding it.
Comments
Post a Comment