Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 01, 2012 04:25 PM by Mikesdotnetting
Member
21 Points
14 Posts
Mar 01, 2012 01:39 AM|LINK
Hi
I am new to cshtml file and recently downloaded the webmatrix , I want to call a function of javascript when i click on the submit button . Can anyone tell me how to do that i tried this
<script type="text/javascript"> function notEmpty(){ var myTextField = document.getElementById('user_name'); alert("You entered: " + myTextField.value); } </script> <html> <body> @{ if (IsPost) { string user_name = Request["user_name"]; <p>You entered: <br /> User Name: @user_name<br /> } else { <form method="post" action=""> user name:<br /> <input type="text" name="user_name" value="" /><br /> <br /> <input type="submit" value='notEmpty' onclick="notEmpty()" onkeypress="notEmpty()" class="submit" /> </form> } } </body> </html>
Mar 01, 2012 03:01 AM|LINK
or is there anyother way that i run a script as the submit button is pressed
Star
7756 Points
1626 Posts
Mar 01, 2012 03:33 AM|LINK
Yes the way u r using is correct..
Mar 01, 2012 03:45 AM|LINK
vinay13mar Yes the way u r using is correct..
But it is not giving me any alert .
All-Star
154927 Points
19867 Posts
Moderator
MVP
Mar 01, 2012 05:52 AM|LINK
You have unbalanced html tags in your if statement. Use the <text> tags to tidy that up and it should work:
<script type="text/javascript"> function notEmpty(){ var myTextField = document.getElementById('user_name'); alert("You entered: " + myTextField.value); } </script> <html> <body> @{ if (IsPost) { string user_name = Request["user_name"]; <text><p>You entered: <br /> User Name: @user_name<br /></text> } else { <form method="post" action=""> user name:<br /> <input type="text" name="user_name" value="" /><br /> <br /> <input type="submit" value='notEmpty' onclick="notEmpty()" onkeypress="notEmpty()" class="submit" /> </form> } } </body> </html>
Mar 01, 2012 03:24 PM|LINK
@Mikesdotnetting Thanks for the reply , but its still not working after adding <text> tag and i am not getting any alert
Mar 01, 2012 04:25 PM|LINK
Oops. You haven't given the input an id for the getElementById function to work with (although IE9 forgives that):
<input type="text" name="user_name" id="user_name" value="" />
uahmed
Member
21 Points
14 Posts
using Javascript in cshtml
Mar 01, 2012 01:39 AM|LINK
Hi
I am new to cshtml file and recently downloaded the webmatrix , I want to call a function of javascript when i click on the submit button . Can anyone tell me how to do that i tried this
<script type="text/javascript"> function notEmpty(){ var myTextField = document.getElementById('user_name'); alert("You entered: " + myTextField.value); } </script> <html> <body> @{ if (IsPost) { string user_name = Request["user_name"]; <p>You entered: <br /> User Name: @user_name<br /> } else { <form method="post" action=""> user name:<br /> <input type="text" name="user_name" value="" /><br /> <br /> <input type="submit" value='notEmpty' onclick="notEmpty()" onkeypress="notEmpty()" class="submit" /> </form> } } </body> </html>uahmed
Member
21 Points
14 Posts
Re: using Javascript in cshtml
Mar 01, 2012 03:01 AM|LINK
or is there anyother way that i run a script as the submit button is pressed
vinay13mar
Star
7756 Points
1626 Posts
Re: using Javascript in cshtml
Mar 01, 2012 03:33 AM|LINK
Yes the way u r using is correct..
V.K.Singh
uahmed
Member
21 Points
14 Posts
Re: using Javascript in cshtml
Mar 01, 2012 03:45 AM|LINK
But it is not giving me any alert .
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: using Javascript in cshtml
Mar 01, 2012 05:52 AM|LINK
You have unbalanced html tags in your if statement. Use the <text> tags to tidy that up and it should work:
<script type="text/javascript"> function notEmpty(){ var myTextField = document.getElementById('user_name'); alert("You entered: " + myTextField.value); } </script> <html> <body> @{ if (IsPost) { string user_name = Request["user_name"]; <text><p>You entered: <br /> User Name: @user_name<br /></text> } else { <form method="post" action=""> user name:<br /> <input type="text" name="user_name" value="" /><br /> <br /> <input type="submit" value='notEmpty' onclick="notEmpty()" onkeypress="notEmpty()" class="submit" /> </form> } } </body> </html>Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
uahmed
Member
21 Points
14 Posts
Re: using Javascript in cshtml
Mar 01, 2012 03:24 PM|LINK
@Mikesdotnetting Thanks for the reply , but its still not working after adding <text> tag and i am not getting any alert
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: using Javascript in cshtml
Mar 01, 2012 04:25 PM|LINK
Oops. You haven't given the input an id for the getElementById function to work with (although IE9 forgives that):
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter