How to pass var to JS

What's wrong with this?

    msg = "Hello"

    Response.Write("<input type='button' onclick='alertl(<%=msg%>)' value='Hil'>")
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Deepak SubburajanDB DeveloperCommented:
Try this.

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

Open in new window

Big MontySenior Web Developer / CEO of 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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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

Should be

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 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 MVEDeveloper & EE ModeratorCommented:
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
msg = "Hello"


<!DOCTYPE html>
<script src="//"></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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.