How to pass value to a onclick function in (Jade)pug?
Asked Answered
B

9

14

I am new to jade and stuck on this issue. I think I have tried everything from the StackOverflow posts and still at nothing.

The things I have tried

button(type='button' class=' c-btn-blue c-btn-circle c-btn-uppercase' value="Read More" onclick='gotoBlog( #{val.link} )')

Error

1:8 Uncaught SyntaxError: Invalid or unexpected token

Changing it to !{val.link}

Error

Uncaught SyntaxError: Unexpected token .

Changing it to "!{val.link}" and "#{val.link}" just gives me string understandably so. BTW val.link is a string

Just giving val.link says Uncaught ReferenceError: val is not defined

I am out of options now. Help will be appreciated.

Thanks

Blow answered 26/8, 2016 at 7:5 Comment(1)
Where are you setting/defining val?Ashti
A
19

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 + ')')
Ashti answered 26/8, 2016 at 20:52 Comment(0)
D
9

I've just used the code with pre and pos quotes and it worked:

button(type='button', onclick='someFunction("'+ yourObject.id +'")' ) PressMe
Darra answered 20/11, 2018 at 21:24 Comment(1)
only this solution worked with meKlaraklarika
M
2

You just need to put onclick="myfunction(#{varible.atributo})"

Here a example:

table
thead
    tr
        th #ID
        th Description
        th Actions
tbody
    each item, i in itemlist
        tr
            th(scope='row') #{item.id}
            td #{item.description}
            td
                button(onclick="editItem(#{item.id})", title="Edit")
                    |  Edit
Monahan answered 27/11, 2016 at 18:7 Comment(0)
O
2

Use differing nested quotation marks so that you pass a string to your gotoBlog function. Here, I use single ticks within double ticks.

button(type='button' class=' c-btn-blue c-btn-circle c-btn-uppercase' value="Read More" onclick="gotoBlog( '#{val.link}' )")

In other words:

button( onclick= "myFunction('#{stringVariable}')" )
Omland answered 6/11, 2017 at 21:1 Comment(0)
C
2

Too late, I know :(

But, this could be useful!

`${}`

So the code will be

button(type='button' class=' c-btn-blue c-btn-circle c-btn-uppercase' value="Read More" onclick=`gotoBlog( ${val.link} )`)
Corkage answered 22/11, 2020 at 15:30 Comment(0)
E
2

Too late but most of the options above didn't work for me but this did

button(onclick='gotoBlog('+'"'+val.link+'"'+')')

or simpler:

onclick=`gotoBlog('${val.link}')`

Basically converts val.link to a string.

Eliezer answered 12/8, 2021 at 18:33 Comment(2)
I also found the answers above did not work for me, yours did, thanksVite
This is the one...Unsecured
J
0

I came across a similar issues and solved it rather differently (by escaping params). In my case, I needed to pass the following template values to a javascript function as argument when a button is clicked

{
  url:"http://google.com",
  token: "Bearer your-token", 
  accountId: "abc123"
}

So the pug in my case looked as follow

button(onclick='authenticate(\'' + url + '\',\'' + token + '\',\'' + accountId + '\')') Login

And the resulting html is as follow

<button onclick="authenticate('http://google.com','Bearer your-token','abc123')">Login</button>
Jargonize answered 15/5, 2018 at 0:16 Comment(0)
B
0

When using multiple parameters in a function, this did the trick:

'myFunction(' + '"' + varA + '"' + ',' + '"' + varB + '"' + ')'

NOTE: outer/inner/all quote marks can be ' (single) or " (double) quote marks, I used single and double quote marks for readability.

Bathroom answered 4/9, 2018 at 23:47 Comment(0)
J
0

button(type='button', onClick='function(\'' + someValue + '\')') Text

This worked for me to use values that I passed to pug inside an onClick function.

Jetty answered 3/11, 2019 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.