[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

How to pass var to JS

What's wrong with this?

    msg = "Hello"

    Response.Write("<input type='button' onclick='alertl(<%=msg%>)' value='Hil'>")
2 Solutions
Deepak SubburajanCommented:
Try this.

Response.Write("<input type='button' onclick='alertl('" + msg + "')' value='Hil'>") 

Open in new window

Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
You have a typo in the word alert and need quotes around the variable

 Response.Write("<input type='button' onclick='alert("<%=msg%>")' value='Hil'>")
JElsterAuthor Commented:
Invalid character

Response.Write("<input type='button' onclick='alert("<%=msg
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
Haven't had my morning caffeine yet sorry :)

Should be

Response.Write("<input type='button' onclick='alert(""" & msg & """)' value='Hil'>")
JElsterAuthor Commented:
Me either...


Uncaught TypeError: string is not a function

looks like this

<input type='button' onclick='alert("Hello")' value='Hil'>
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
did you get it to work? i just tested the exact code on my end and it's working...

why was the assisted question selected as a valid answer? it is syntactically incorrect both on the server side and on the javascript it generates, and will not work.
JElsterAuthor Commented:
no..  doesn't like the alert. ??????????????????
console.log works  - I don't need the alert. just need to call a function.
Thought the other code was ok, sorry
thx again for your help
Scott Fell, EE MVEDeveloperCommented:
Looking at your question as to what you asked for, BM's answer http:Q_28502550.html#a40276080 is the correct way out of the two accepted.  The first answer by Deepak Subburajan will render as below
<input type="button" onclick="alert"1(" hello')' value="Hil">

Open in new window

and it should be
<input type="button" onclick="alert"(" hello")" value="Hil">

Open in new window

2 errors in the first.  the 1 before the (" hello and the single/double quoting is wrong.

Overall, you probably want to get in the habit of using button now instead of input type="button".  And keep your functions in your js.  Below would be my take on modernizing what we do in asp.  Static sample http://jsbin.com/gahozi/1/edit
msg = "Hello"


<!DOCTYPE html>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
        var say=$(this).attr('data-item');
  <meta charset="utf-8">
<button class="test" data-item="hello">Hil</button>
response.write "<button class='test' data-item='"&msg&"'>Hil</button>"

Open in new window


Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now