How to pass var to JS

Hi..
What's wrong with this?


    msg = "Hello"

    Response.Write("<input type='button' onclick='alertl(<%=msg%>)' value='Hil'>")
LVL 1
JElsterAsked:
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

0
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'>")
0
JElsterAuthor Commented:
Invalid character


Response.Write("<input type='button' onclick='alert("<%=msg
------------------------------------------------------^
0
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 ExchangeTree.org Commented:
Haven't had my morning caffeine yet sorry :)

Should be

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

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...

Get

Uncaught TypeError: string is not a function

looks like this

<input type='button' onclick='alert("Hello")' value='Hil'>
0
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.
0
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
0
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 http://jsbin.com/gahozi/1/edit
<%
msg = "Hello"

%>

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
$(function(){
	$('.test').on('click',function(){
        var say=$(this).attr('data-item');
        alert(say);
	});
});	
</script>
  <meta charset="utf-8">
  <title>test</title>
</head>
<body>
<button class="test" data-item="hello">Hil</button>
<%
response.write "<button class='test' data-item='"&msg&"'>Hil</button>"
%>
</body>
</html>

Open in new window

0
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
ASP

From novice to tech pro — start learning today.