Solved

HTML button in a form stops working in Visual Studio

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I do this in Angular, instead of jQuery? 2 22
Anchor Bookmark 6 34
Javascript Error 10 24
Allow the whole div to submit form not just button 2 10
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

739 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