Solved

HTML button in a form stops working in Visual Studio

Posted on 2014-09-22
3
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Find out what you should include to make the best professional email signature for your organization.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

740 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