473,378 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

onblur add dynamic form does not work

Hi guys,

I'm a javascript beginner and I am trying to write this simple javascript code that will add text field when I type some texts in the preexisting text field. When the text field is onblur, javascript will see if the type of the text's value is either number or string.
Depending on its type it will create either a text field or a button. However, for some reason, nothing is happening. Could you check my code and see what's wrong with it? Thank you.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" >
  2. var defaultname = "workout";
  3. var defaultnumber = 0;
  4. var forms2;
  5. var text1;
  6. var text1type;
  7.  
  8. function addform() {
  9.     defaultnumber++;
  10.     text1 = document.getElementsByName("copy");
  11.     text1type = typeof text1.value;
  12.     if (text1type == "number") {
  13.         forms2 = document.createElement("input");
  14.         forms2.type = "text";
  15.         forms2.name = defaultname + defaultnumber;
  16.         inHere = document.getElementById("inhere");
  17.         inHere.appendChild(forms2);
  18.     } elseif (text1type == "string") {
  19.         forms2 = document.createElement("input");
  20.         forms2.type = "button";
  21.         forms2.name = defaultname + defaultnumber;
  22.         forms2.value = "button";
  23.         inHere = document.getElementById("inhere");
  24.         inHere.appendChild(forms2);
  25.     } else document.write("wrong input");
  26.  
  27. }
  28.  
  29. </script>

[HTML]
<div id="add" >
<form action="addformtest2.html" method="post" name="addformtest" >
<input type="text" onblur="addform()" name="copy" value="" />
<div id="inhere"> </div>
</form>
</div>
[/HTML]
Dec 5 '08 #1
1 2373
RamananKalirajan
608 512MB
Hi, there were couple of problems in your code.

1) in else if condition, there is no space between the else and if.
2) Instead of document.getElementsByTagName use document.getElementById.


Moreover any input will return only the string value so in ur code, u will be able to see a button creating after blur event..

The following is one logic, but try yours by own

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript" > 
  4. var defaultname = "workout"; 
  5. var defaultnumber = 0; 
  6. var forms2; 
  7. var text1; 
  8. var text1type; 
  9.  
  10. function addform() { 
  11.     defaultnumber++; 
  12.     text1 = document.getElementById("copy"); 
  13.     text1type = checkNo(text1.value);
  14.     //alert("Value = "+text1type);
  15.     if (text1type == "number") { 
  16.         forms2 = document.createElement("input"); 
  17.         forms2.type = "text"; 
  18.         forms2.name = defaultname + defaultnumber; 
  19.         inHere = document.getElementById("inhere"); 
  20.         inHere.appendChild(forms2); 
  21.     } else if (text1type == "string") { 
  22.         forms2 = document.createElement("input"); 
  23.         forms2.type = "button"; 
  24.         forms2.name = defaultname + defaultnumber; 
  25.         forms2.value = "button"; 
  26.         inHere = document.getElementById("inhere"); 
  27.         inHere.appendChild(forms2); 
  28.     } else document.write("wrong input"); 
  29.  
  30.  
  31. function checkNo(val)
  32. {
  33.      var no=new Array("1","2","3","4","5","6","7","8","9");
  34.      var flag=1;
  35.      var x;
  36.      for(var i=0;i<no.length;i++)
  37.      {
  38.             x=val.indexOf(no[i]);
  39.             if(x>-1)
  40.             {
  41.              flag=0;
  42.              break;
  43.             }
  44.      }
  45.      if(flag==0)
  46.      {
  47.         return "number";
  48.      }
  49.      else
  50.      {
  51.        return "string";
  52.      }
  53. }
  54.  
  55.  
  56. </script> 
  57. </head>
  58. <body>
  59. <div id="add" >
  60. <form action="addformtest2.html" method="post" name="addformtest" >
  61. <input type="text" onblur="addform()" name="copy" value="" />
  62. <div id="inhere"> </div>
  63. </form>
  64. </div>
  65. </body>
  66. </html>

Regards
Ramanan Kalirajan
Dec 5 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bartosz Wegrzyn | last post by:
I use onblue event to validate fields in my form. I do this onblur="return isname()" and so so ... I have one form with 20 fields. My problem is that when the focus is for example on the...
9
by: Roger Withnell | last post by:
Tearing hair out time! Simple attached page shows the problem. http://www.brilley.co.uk/TestFocusSelect.htm Using a function to test if too many characters have been keyed in to a textarea....
1
by: Christoph | last post by:
I'm trying to validate some HTML form elements when the user tabs out of each element. However, I'm having some problems. It appears that the order of events is onChange followed some time...
2
by: andyalean | last post by:
Hello javascript coders :( ,I am trying to add an onblur event to my code. This is where I dynamically create a textfield.I want to assign it an onblur event handler like so.How do I add a event...
1
by: neil S via DotNetMonster.com | last post by:
I have a custom control with a textbox and dropdown list. The dropdown list is hidden and acts as a data source for the textbox. When the user enters text in the textbox, an onKeyup event is...
4
by: josh.dutcher | last post by:
I'm trying to set up a simple "change your password" form. I've got an AJAX process that is checking the new password the user enters against their current password to make sure they're changing...
10
by: John Kotuby | last post by:
Hello all... I am working on an ASP.NET 2.0 application with VS2005 and VB. I have chosen to use popup windows in some cases because it makes the user experience better (according to all the users...
1
by: Duncan | last post by:
Hi all, I'm tearing my hair out on this! I have a simple ASP.NET 2 page with a TextBox control, in the PreRender I set ClientSide events:- Response.Attributes.Add("onchange",...
2
by: John Kotuby | last post by:
Hi all, I am integrating some 3rd party grids into a large ASPX form page in VS 2008 using VB. I have 4 different grids on the page. The object is to not allow the user to leave a grid if...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.