Solved

HTML button in a form stops working in Visual Studio

Posted on 2014-09-22
3
323 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 82

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Summernote and form validation 10 42
Modify Table Width 6 13
Jump to a specific point on a responsive page 5 13
Asp.net mvc 5 5 19
This article discusses how to create an extensible mechanism for linked drop downs.
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 …
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
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)

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now