How to pass value to a onclick function in (Jade)pug?
When adding attributes to an html element, you are already within the scope of pug, so you can just use pug variables like regular js variables.
button(type='button' class=' c-btn-blue c-btn-circle c-btn-uppercase' value="Read More" onclick='gotoBlog(' + val.link + ')')
In Jade, how to pass parameters in the onclick function call
The problem here is that javascript is expecting quotes around functions. What jade is making is <a id='myLinkTag' onclick='myFunction(userid) href=''>
This is not what javascript is expecting, Javascript expects a valid variable name to be given to its onclick functions. There are two choices:
If you are SURE that there is NO WAY for user._id to ever be anything other than a string ONLY CONTAINING alphanumeric characters (
new RegExp(/^(A-Za-z0-9)+$/)
) then put quotes in myFunction:a(id='myLinkTag' onclick='myFunction(\'' + user._id + '\')' href='') Delete
If user._id can be more than alphanumeric characters, you can instead use jade to set a class and a custom attribute like
a(id='myLinkTag' class="myclass" userid=user._id href='') Delete
then in javascript you can:
:
$(".myclass").click(function(e){ var userid = $(e.target).attr("userid"); console.log(userid); alert(userid);});
Jade would be:
a(id='myLinkTag' class="myclass" userid=user._id href='#')a(id='myLinkTag2' class="myclass" userid=user2._id href='#')a(id='myLinkTag3' class="myclass" userid=user3._id href='#')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><a class="myclass" userid="myuserid" href="#">link</a><a class="myclass" userid="myuserid2" href="#">link2</a><a class="myclass" userid="myuserid3" href="#">link3</a>
Button onClick using .jade with a JS function
Your issue comes from mixing server-side pug/jade and client-side JavaScript.
Let's focus on your button code:
button(onClick = #{screenshots(client)}) GO
The #{}
is a pug instruction to insert the value of a variable into that space. At the time the button is pressed you just want it to call a local script, and no pug/jade is needed for that:
button(onClick= "screenshots(client)") GO
The error code you get is due to trying to call a function where pug/jade is expecting an expression that it can evaluate to a string.
I can't be sure as I don't know how your file system is set up, but you probably need to reference the script using an absolute path:
script (src='/scripts/screenshot.js')
Pass variable to onClick Function
Your concatenation is fine (as long as strDocument
has a value that when concatenated with the static text forms a valid URL).
But, this is much simpler when it comes to <a>
elements - no onclick
needed because <a>
elements can target new windows with the target
attribute.
function getLink(){ var someData = "foo"; return "some/path/" + someData; console.log(link.href);}
<a href="javascript:getLink()" target="_blank" id="testLink">test</a>
Onclick function pug from node
The res.render statement is only meant to send data to the template, you can't send a function through there. The Express documentation explains this.
The function is running when you request the page because that's actually what you're telling it to do when you include that function in there.
You will need to write some client-side JavaScript to make an AJAX call or form POST then create a new ExpressJS route on the server to capture that and run the function.
Related Topics
Display a Loading Icon While Images Loads
How to Intercept Button Click Inside Uiwebview on Ios
Form Builder - Cannot Read Property 'Get' of Undefined, Issue With Validators
Call JavaScript Function Onchange Event of Dropdown List
Replacing Multiple Elements of an Array While Keeping Their Position
404 Error When Trying to Register Serviceworker
How to Format a Number to 2 Decimal Places, But Only If There Are Already Decimals
Combining JavaScript and CSS into HTML File
How to Override the Onbeforeunload Dialog and Replace It With My Own
How to Send Secure Parameter in Ajax Request
How to Hide the Android Keyboard Using JavaScript
Javascript Random on Array Without Repeat
Get Selected Value from Multiple Select on Change in Dynamic Form
How to Sort Json Array Elements in Descending Order
How to Use Fetch Within a For-Loop, Wait for Results and Then Console.Log It