473,387 Members | 1,517 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,387 software developers and data experts.

Dynamic CreateElement add onblur?

Hello javascript coders :( ,I am trying
to add an [b:be2ca59534]onblur[/b:be2ca59534] event to my code.
This is where I dynamically create a textfield.I want to assign it an
[b:be2ca59534]onblur[/b:be2ca59534] event handler like so.How do I
add a event to a newly created option text field.Thanks :D

for( ;k < holdHalfHours; k++ ){
timeLog.appendChild(br);
l = k +1;

var newHoldDesc = document.createTextNode( "Hold
Half Hour" + l );

timeLog.appendChild( newHoldDesc );
var inputHoldText =
document.createElement("input");

inputHoldText.type = "text";
inputHoldText.size = "5";
inputHoldText.id = "txtfHolding" + l;
inputHoldText.onblur = "checkValue(this);";
timeLog.appendChild(inputHoldText);

timeLog.appendChild( newSpacer
);

Developers Forum - @ DevPlug.com
Jul 23 '05 #1
2 11375
Ovi
I have a nice example which might help you outthere, here goes...

###############################Start

<html>
<head>

<script>
function newInput(name, prevId) {
var id = 1 + prevId;
var newDiv = document.createElement('div');
var newInput = document.createElement('input');
newInput.name = name;
newInput.id = id;
newDiv.appendChild(newInput);
document.getElementById('inputList').appendChild(n ewDiv);
newInput.focus();
load();
}
function move(element, distance) {
var parent = element.parentElement;
var index = 0;
for (var i = 0; i < parent.children.length; i++) {
if (parent.children[i] == element) {
index = i;
}
}
if (element.value != '') {
if (index == parent.children.length - 1) {
if (distance > 0) {
return true;
} else {
newInput(element.name, element.id);
}
}
if (parent.children[index + distance].value != '' && index + distance >
0) {
parent.removeChild(element);
parent.insertBefore(element, parent.children[index + distance]);
parent.children[index + distance].focus();
}
}
}
function keyPressed(e) {
if (!e) {
e = window.event;
var target = e.srcElement;
var key = e.keyCode;
} else {
var target = e.target;
var key = e.which;
}
var div = target.parentElement;
if (key == 38) {
move(div, -1);
} else if (key == 40) {
move(div, 1);
}
return true;
}
function lostFocus(e) {
if (!e) {
e = window.event;
var target = e.srcElement;
} else {
var target = e.target;
}
if (target.type == 'text') {
var id = 0;
var inputs = document.getElementsByTagName('input');
if (inputs[inputs.length - 1] == target) {
if (target.value.length > 0) {
newInput(target.name, target.id);
}
} else {
if (target.value.length == 0) {
document.getElementById('inputList').removeChild(t arget.parentElement)
}
}
}
return true;
}
function load() {
var x = document.forms[0].elements;
for (var i=0;i<x.length;i++) {
x[i].onblur = function (e) {lostFocus(e)};
x[i].onkeyup = function (e) {keyPressed(e)};
}
}
</script>

</head>
<body onload="newInput('test', 0); load();">
<form name="whatever" ID="Form1">
<input type="submit" value="Save" NAME="submit" ID="Submit1">
<div id="inputList">
</div>
</form>
</body>
</html>

###############################End


andyalean wrote:
Hello javascript coders :( ,I am trying
to add an [b:be2ca59534]onblur[/b:be2ca59534] event to my code.
This is where I dynamically create a textfield.I want to assign it an
[b:be2ca59534]onblur[/b:be2ca59534] event handler like so.How do I
add a event to a newly created option text field.Thanks :D

for( ;k < holdHalfHours; k++ ){
timeLog.appendChild(br);
l = k +1;

var newHoldDesc = document.createTextNode( "Hold
Half Hour" + l );

timeLog.appendChild( newHoldDesc );
var inputHoldText =
document.createElement("input");

inputHoldText.type = "text";
inputHoldText.size = "5";
inputHoldText.id = "txtfHolding" + l;
inputHoldText.onblur = "checkValue(this);";
timeLog.appendChild(inputHoldText);

timeLog.appendChild( newSpacer
);

Developers Forum - @ DevPlug.com


Jul 23 '05 #2
andyalean wrote:
Hello javascript coders :( ,I am trying
to add an [b:be2ca59534]onblur[/b:be2ca59534] event to my code.
This is where I dynamically create a textfield.I want to assign it an
[b:be2ca59534]onblur[/b:be2ca59534] event handler like so.How do I
add a event to a newly created option text field.Thanks :D
When posting code, do not use tabs. Replace them with two or four
spaces and manually wrap code at about 70 character to allow a couple
of re-posts without automatic wrapping.

for( ;k < holdHalfHours; k++ ){
timeLog.appendChild(br);
l = k +1;

var newHoldDesc = document.createTextNode( "Hold
Half Hour" + l );

timeLog.appendChild( newHoldDesc );
var inputHoldText =
document.createElement("input");

inputHoldText.type = "text";
inputHoldText.size = "5";
Creating inputs using DOM methods can be problematic in some
browsers, test thoroughly.
inputHoldText.id = "txtfHolding" + l;
inputHoldText.onblur = "checkValue(this);";


Guessing that 'chekValue()' is a function defined elsewhere, then:

inputHoldText.onblur = function () {checkValue(this)};

should do the trick.

[...]

--
Rob
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Cindy | last post by:
hi, Can someone point me to a website where I can find a "create dynamic rows" javascript that work for IE, NS6 & NS7? Thank You. regards, Cindy
7
by: Cindy | last post by:
"kaeli" <infinite.possibilities@NOSPAMatt.net> wrote in message news:MPG.197482677bbc284b989702@nntp.lucent.com... > In article <bed8e3$m6r$1@mawar.singnet.com.sg>, stayhardsg@yahoo.com.sg >...
1
by: Tzachi | last post by:
Hello all, I have a function that dynamically adds rows and columns to the page. Everything works well except onfocus // onblur attributes. For some reason, when entering the input box it...
2
by: The_Original_MB | last post by:
I have a task to create tables dynamically, using the javascript DOM. The current method uses a 1px x 1px IFRAME to loop through some data generation stuff, and then call JS functions in the parent...
5
by: pizzy | last post by:
I am having a problem with the last results. I can't seem to be able to get the input2A and input3A to appear. I don't seem to have a problem with the show and hide after a number is entered and...
1
by: rubenhan | last post by:
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...
1
by: aashishn86 | last post by:
i create dynamic rows using this function : function addRow() { var tbl = document.getElementById('applications'); var lastRow = tbl.rows.length; var iteration = lastRow; var row =...
2
by: yomadhu | last post by:
I created a dynamic form in javascript. Am unable to get those values in to php to display. I need all details. If i add 10 rows the i need to display those all values. Can any one help me for that...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.