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:

codepen weather app

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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -