Solved

javascript not working

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article discusses four methods for overlaying images in a container on a web page
In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

840 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