Solved

javascript not working

Posted on 2011-09-08
4
201 Views
Last Modified: 2012-05-12
I write the simple form display(hide and show) use in javascript..this form output not display in google chrome..IE and firefox working correctly,. but chrome not display the output..what is the mistake in my program..plz send me comments..here i paste in my html and js file..
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dynamic Order Form</title>
<script language="JavaScript" type="text/javascript"
src="dform.js">
</script>
</head>
<body>
<h1>Order Form</h1>
<hr>
<form name="form1">
  <b>Bill to:</b><br>
  <b>Name:</b>
  <input type="text" name="customer" size="20" id="txt">
  <br>
  <b>Address 1:</b>
  <input type="text" name="addr1" size="20">
  <br>
  <b>Address 2:</b>
  <input type="text" name="addr2" size="20">
  <br>
  <b>City:</b>
  <input type="text" name="city" size="15">
  <b>State:</b>
  <input type="text" name="state" size="4">
  <b>Zip:</b>
  <input type="text" name="zip" size="9">
  <hr>
  <b>Ship to:</b><br>
  <input type="radio" name="shipopt" value="same" checked onClick="Show(0);">
  <b>Same Address</b>
  <input type="radio" name="shipopt" value="other" onClick="Show(1);">
  <b>Other Address</b>
  <div ID="shipto" style="display: none;"> <br>
    <b>Name:</b>
    <input type="text" name="shipname" size="20">
    <br>
    <b>Address 1:</b>
    <input type="text" name="shipaddr1" size="20">
    <br>
    <b>Address 2:</b>
    <input type="text" name="shipaddr2" size="20">
    <br>
    <b>City:</b>
    <input type="text" name="shipcity" size="15">
    <b>State:</b>
    <input type="text" name="shipstate" size="4">
    <b>Zip:</b>
    <input type="text" name="shipzip" size="9">
  </div>
  <hr>
  <a href="#" onClick="toggle_visibility('foo');">Click here to toggle visibility of element #foo</a>
  <div id="foo">This is foo</div>
  <div ID="items"> <b>Qty:</b>
    <input type="text" name="qty" size="3">
    <b>Item:</b>
    <input type="text" name="item" size="45">
    <br>
    <input type="button" value="Add an Item" onClick="AddItem();" ID="add">
  </div>
  <hr>
  <input type="submit" value="Continue..." onClick="display();">
</form>
</body>
</html>
__________________________________________________________
// global variable
var items=1;
function AddItem() {
if (!document.getElementById) return;
// Add an item to the form
div=document.getElementById("items");
button=document.getElementById("add");
items++;
newitem="<b>Qty: </b>";
newitem+="<input type=\"text\" name=\"qty" + items;
newitem+="\" size=\"3\"> ";
newitem+="<b>Item: </b>";
newitem+="<input type=\"text\" name=\"item" + items;
newitem+="\" size=\"45\"><br>";
newnode=document.createElement("span");
newnode.innerHTML=newitem;
div.insertBefore(newnode,button);
}
function display()
{
	var dis;
	dis=document.form1.customer.value+ "</br>";
	dis+=document.form1.addr1.value+ "</br>";
	dis+=document.form1.addr2.value+ "</br>";
	dis+=document.form1.city.value+ "</br>";
	dis+=document.form1.state.value+ "</br>";
	dis+=document.form1.zip.value+ "</br>";
	obj=document.getElementById("shipto");
	if(obj.style.display=="block") 
	{
	dis+=document.form1.shipname.value+ "</br>";
	dis+=document.form1.shipaddr1.value+ "</br>";
	dis+=document.form1.shipaddr2.value+ "</br>";
	dis+=document.form1.shipcity.value+ "</br>";
	dis+=document.form1.shipstate.value+ "</br>";
	dis+=document.form1.shipzip.value+ "</br>";
	document.write(dis);
	document.close();
	}
	else
	{
	document.write(dis);
	document.close();
	}	

}
function toggle_visibility(id) {
       var e = document.getElementById(id);
      if(e.style.display == 'block')
         e.style.display = 'none';
      else
       e.style.display = 'block';
}
function Show(a) {
//if (!document.getElementById) return;  //hide i
//Hide or show ship-to address
obj=document.getElementById("shipto");
if (a) obj.style.display="block";
else obj.style.display="none";
}

Open in new window

0
Comment
Question by:codeoxygen
  • 2
4 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 36508106
change:
<script language="JavaScript" type="text/javascript"
src="dform.js">
</script>

To:
<script type="text/javascript" src="dform.js"></script>

===========
change:
<a href="#" onClick="toggle_visibility('foo');">

to:
<a href="#" onclick="return toggle_visibility('foo');">

===========
function toggle_visibility(id) {...}

needs return false; at the end

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dynamic Order Form</title>
<script type="text/javascript" >
// global variable
var items=1;
function AddItem() {
if (!document.getElementById) return;
// Add an item to the form
div=document.getElementById("items");
button=document.getElementById("add");
items++;
newitem="<b>Qty: </b>";
newitem+="<input type=\"text\" name=\"qty" + items;
newitem+="\" size=\"3\"> ";
newitem+="<b>Item: </b>";
newitem+="<input type=\"text\" name=\"item" + items;
newitem+="\" size=\"45\"><br>";
newnode=document.createElement("span");
newnode.innerHTML=newitem;
div.insertBefore(newnode,button);
}
function display()
{
	var dis;
	dis=document.form1.customer.value+ "</br>";
	dis+=document.form1.addr1.value+ "</br>";
	dis+=document.form1.addr2.value+ "</br>";
	dis+=document.form1.city.value+ "</br>";
	dis+=document.form1.state.value+ "</br>";
	dis+=document.form1.zip.value+ "</br>";
	obj=document.getElementById("shipto");
	if(obj.style.display=="block") 
	{
	dis+=document.form1.shipname.value+ "</br>";
	dis+=document.form1.shipaddr1.value+ "</br>";
	dis+=document.form1.shipaddr2.value+ "</br>";
	dis+=document.form1.shipcity.value+ "</br>";
	dis+=document.form1.shipstate.value+ "</br>";
	dis+=document.form1.shipzip.value+ "</br>";
	document.write(dis);
	document.close();
	}
	else
	{
	document.write(dis);
	document.close();
	}	

}
function toggle_visibility(id) {
       var e = document.getElementById(id);
      if(e.style.display == 'block')
         e.style.display = 'none';
      else
       e.style.display = 'block';
return false;
}
function Show(a) {
//if (!document.getElementById) return;  //hide i
//Hide or show ship-to address
obj=document.getElementById("shipto");
if (a) obj.style.display="block";
else obj.style.display="none";
}
</script>
</head>
<body>
<h1>Order Form</h1>
<hr>
<form name="form1">
  <b>Bill to:</b><br>
  <b>Name:</b>
  <input type="text" name="customer" size="20" id="txt">
  <br>
  <b>Address 1:</b>
  <input type="text" name="addr1" size="20">
  <br>
  <b>Address 2:</b>
  <input type="text" name="addr2" size="20">
  <br>
  <b>City:</b>
  <input type="text" name="city" size="15">
  <b>State:</b>
  <input type="text" name="state" size="4">
  <b>Zip:</b>
  <input type="text" name="zip" size="9">
  <hr>
  <b>Ship to:</b><br>
  <input type="radio" name="shipopt" value="same" checked onClick="Show(0);">
  <b>Same Address</b>
  <input type="radio" name="shipopt" value="other" onClick="Show(1);">
  <b>Other Address</b>
  <div ID="shipto" style="display: none;"> <br>
    <b>Name:</b>
    <input type="text" name="shipname" size="20">
    <br>
    <b>Address 1:</b>
    <input type="text" name="shipaddr1" size="20">
    <br>
    <b>Address 2:</b>
    <input type="text" name="shipaddr2" size="20">
    <br>
    <b>City:</b>
    <input type="text" name="shipcity" size="15">
    <b>State:</b>
    <input type="text" name="shipstate" size="4">
    <b>Zip:</b>
    <input type="text" name="shipzip" size="9">
  </div>
  <hr>
  <a href="#" onclick="return toggle_visibility('foo');">Click here to toggle visibility of element #foo</a>
  <div id="foo">This is foo</div>
  <div ID="items"> <b>Qty:</b>
    <input type="text" name="qty" size="3">
    <b>Item:</b>
    <input type="text" name="item" size="45">
    <br>
    <input type="button" value="Add an Item" onClick="AddItem();" ID="add">
  </div>
  <hr>
  <input type="submit" value="Continue..." onClick="display();">
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:codeoxygen
ID: 36508237

Now function toggle_visibility(id) {...} working good,. but display() function not working in google chrome and data not print in another page(document.write(dis)) in google chrome...
0
 
LVL 82

Accepted Solution

by:
hielo earned 250 total points
ID: 36511815
Try:
 <input type="button" value="Continue..." onClick="display();">
0
 
LVL 9

Expert Comment

by:user_n
ID: 36512583
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating a slider 12 44
Help cleaning out CSS 2 50
jQuery detect if it is a mobile device 3 90
Having a little issue with EDGE/IE CSS display of after 2 30
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

947 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

22 Experts available now in Live!

Get 1:1 Help Now