Joaquin
Joaquin

Reputation: 43

How to pass a string on a onclick method

I have a html like this

<button class="btn btn-success" data-dismiss="modal" 
onclick="updatepage(TestString)" data-toggle="modal">Test String</button>

and the function is

function updatepage(variable){
 // some stuff
}

I'm getting an error "TestString is not defined", and if I put

onclick="updatepage("TestString")" 

I get a syntax error.

Any ideas what I might be doing wrong?

Upvotes: 0

Views: 537

Answers (3)

Halcyon
Halcyon

Reputation: 57709

onclick="updatepage(" + escape(JSON.stringify(TestString)) + ")"

You should consider using the EventListener API. This notation is limited to values that are easily serializable. You can not pass-by-reference, for instance.

Upvotes: 0

Praveen
Praveen

Reputation: 56501

You can try this

onClick="updatepage(\'' + TestString + '\')" 

Upvotes: 1

Zbigniew
Zbigniew

Reputation: 27584

You can use single quotes as string delimiters:

onclick="updatepage('TestString')"

Upvotes: 2

Related Questions