Solved

javascript not working

Posted on 2011-09-08
4
202 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Max-width on individual images in grid 4 55
Syntax Error 2 62
bootstrap footer centering and expand problems 7 42
Change div area and length 1 26
"I want to put my photos online, but I don't want them stolen.  What settings should I use?" When You Put Photos Online First and foremost, any digital file published on the WWW can be copied, stored, modified, retransmitted, etc.  Remember Naps…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…

813 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

13 Experts available now in Live!

Get 1:1 Help Now