Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

HTML button in a form stops working in Visual Studio

Posted on 2014-09-22
3
Medium Priority
?
336 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 1000 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

916 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