Solved

HTML button in a form stops working in Visual Studio

Posted on 2014-09-22
3
325 Views
Last Modified: 2014-09-22
I created a web site in Visual Studio and added an HTML page (not .aspx web form) to the site. The page has a button that puts the word "Hello" in a text box as shown below.

<html>
<head>
    <title></title>
    <script>
        function SayHello() {
            document.getElementById('button1').value = "Hello";
        }
    </script>
</head>
<body>
    <input type="text"id="button1" /> <br/><br/>
    <input type="button" value="OK" onclick="SayHello()"/>
</body>
</html>

However, the button stops working when I put the controls inside a form as shown below.

 <form>
    <input type="text"id="button1" /> <br/><br/>
    <input type="button" value="OK" onclick="SayHello()"/>
 </form>

Changing the button from type="submit" to type="button" did not resolve the issue. Also, this is only a problem in Visual Studio. If you create this same html page in Notepad, the button works fine both with and without the <form> tags.

One solution would be to exclude the form tags. But then I wouldn't be able to copy html and JavaScript code to an .aspx web form becuause in a .aspx form I have to use <form> tags.

Another solution is to user server controls instead of HTML controls. But again, I'd like to be able to copy and paste code from a.html page to a .aspx page.

So is there a way to make this code work in Visual Studio?
 <form>
    <input type="text"id="button1" /> <br/><br/>
    <input type="button" value="OK" onclick="SayHello()"/>
 </form>
0
Comment
Question by:MBarongan
3 Comments
 
LVL 2

Accepted Solution

by:
Vishal Patil earned 250 total points
ID: 40336130
Hello

I checked your code its working fine.
There should not be any issue in this. No need to add Form tag also.

You can again check following :
<!DOCTYPE html>

<html>
<head>
    <title></title>
    <script type="text/javascript">
        function SayHello() {
            document.getElementById('button1').value = "Hello";
            alert(document.getElementById('button1').value);
        }
    </script>
</head>
<body> 
    <input type="text"id="button1" /> <br/><br/> 
    <input type="button" value="OK" onclick="SayHello()"/>
</body>
</html>

Open in new window

One more thing, if possible please check what DocType you have used in your HTML page.
You can use "<!DOCTYPE html>" as HTML doc type in visual studio.
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 40336140
Maybe VS is objecting because your <form> tag is not adequate.  You don't have 'action' or 'method' attributes.  Try this:
<form action="#" method="post">
    <input type="text"id="button1" /> <br/><br/>
    <input type="button" value="OK" onclick="SayHello()"/>
 </form>

Open in new window

0
 

Author Comment

by:MBarongan
ID: 40336289
Thank you both for your responses.

The problem is obviously my Visual Studio. When I dragged a second copy of the html page (has exact same code except it was created in Notepad) from my desktop to my Solution Explorer, the second copy worked. Now I have two copies of the web page in Visual Studio. The 2nd copy works. The 1st copy still doesn't work.  When I paste the code from  the 1st copy to the 2nd copy and vice versa, the results are the same: 2nd one works, 1st one doesn't work. It's a mystery to me. I'm just going to accept that fact that some things about computers can't be explained.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question