Link to home
Start Free TrialLog in
Avatar of JElster
JElsterFlag for United States of America

asked on

How to pass var to JS

Hi..
What's wrong with this?


    msg = "Hello"

    Response.Write("<input type='button' onclick='alertl(<%=msg%>)' value='Hil'>")
SOLUTION
Avatar of Deepak Subburajan
Deepak Subburajan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Big Monty
You have a typo in the word alert and need quotes around the variable

 Response.Write("<input type='button' onclick='alert("<%=msg%>")' value='Hil'>")
Avatar of JElster

ASKER

Invalid character


Response.Write("<input type='button' onclick='alert("<%=msg
------------------------------------------------------^
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JElster

ASKER

Me either...

Get

Uncaught TypeError: string is not a function

looks like this

<input type='button' onclick='alert("Hello")' value='Hil'>
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.
Avatar of JElster

ASKER

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