473,466 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

dynamic building HTML elements problem

To all,

I'm working on a javascript to dynamic build a common set of HTML
controls.
01) I've used the DOM object to build a <div> tag; then build 1 <input
type='button'/> and 1 <input type='text'> object
02) by using appendChild () method, I've successfully append the button
and the textbox under the div tag (or container in this case)
03) but now I would like to add the javascript onclick event on the
button I've just created...

Problem:
I've managed to add the required Javascript function by this syntax:
aButton.onclick=onclickHandler;
but I can't figure out how to pass in the parameters to the
function!!!! For example I need to pass in a string to identify the
required data format for validation in the onclickHandler function....

I urgently need a work around or the actual way to pass in parameters
to the function dynamically.

Thx!
From Jason (Kusanagihk)


May 16 '06 #1
1 1511
ASM
ku********@gmail.com a écrit :
To all,

I'm working on a javascript to dynamic build a common set of HTML
controls.
01) I've used the DOM object to build a <div> tag; then build 1 <input
type='button'/> and 1 <input type='text'> object
02) by using appendChild () method, I've successfully append the button
and the textbox under the div tag (or container in this case)
03) but now I would like to add the javascript onclick event on the
button I've just created...

Problem:
I've managed to add the required Javascript function by this syntax:
aButton.onclick=onclickHandler;
but I can't figure out how to pass in the parameters to the
function!!!! For example I need to pass in a string to identify the
required data format for validation in the onclickHandler function....
function validate(aform) {
vaf f = aform.elements;
for(var i=0;i<f.length;i++)
if(f[i].type == 'text' && f[i].value == '')
{
alert(f[i].id+' not filled');
return false;
}
return true;
}

<form onsubmit="return validate(this);"

and you have not to worry about your new inputs in your form

I urgently need a work around or the actual way to pass in parameters
to the function dynamically.


Exercise :

<html>
<script type="text/javascript">
function newElemts() {
var ix = 0;
var foo = document.createElement('DIV');
foo.id = 'myFoo';
var newput = document.createElement('INPUT');
newput.id = 'put_'+ix;
newput.type = 'text';
foo.appendChild(newput);
ix++;
var newput = document.createElement('INPUT');
newput.id = 'put_'+ix;
newput.type = 'button';
newput.value = 'to see';
foo.appendChild(newput);
document.body.appendChild(foo);
}
function addClick(what) {
document.getElementById(what).onclick = function() {
alert('my type is : '+this.type);
}
}
function newClick(what) {
$ = function(x) { return document.getElementById(x); }
$(what).onclick = function() {
alert('text field content is :\n'+$('put_0').value);
}
}
</script>
<ol>
<li><a href="#" onclick="newElemts();return false;">new elemts</a>
<li><a href="#" onclick="addClick('put_1');return false;">new event on
button</a>
<li><a href="#" onclick="newClick('put_1');return false;">modify event
on button</a>
</ol>
<html>
Demo :

<html>
<script type="text/javascript">
function validate(aform) {
var f = aform.elements;
for(var i=0;i<f.length;i++)
if(f[i].type == 'text' && f[i].value == '')
{
alert(f[i].id+' not filled');
f[i].focus();
f[i].select();
return false;
}
return true;
}
function newElemts(nber,where) {
where = document.forms[where];
var ix = 0;
while(ix<nber) {
var newput = document.createElement('INPUT');
newput.id = 'put_'+ix;
newput.type = 'text';
where.appendChild(newput);
ix++;
}
}
</script>
<form name="myform" action="javascript:alert('ok')"
onsubmit="return validate(this);">
<ol>
<li><a href="#"
onclick="newElemts(3,'myform');return false;">new elemts</a>
<li><input type=submit value="send">
</ol>
</form>
<html>
--
Stephane Moriaux et son [moins] vieux Mac
May 16 '06 #2

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

Similar topics

1
by: Big Red | last post by:
Hi all I am reasonably new at php. I have a database table with three fields, id, caption and data. I have used the id and caption to create a dynamic menu. it just throws it into a loop with...
1
by: Sophisticado | last post by:
Apologies for x-posting. I am trying to embed dynamic elements collected from a form in a mime mail. The form sends elements to other pages, a database, and a text mail() message; but, I am...
13
by: mr_burns | last post by:
hi, is it possible to change the contents of a combo box when the contents of another are changed. for example, if i had a combo box called garments containing shirts, trousers and hats, when...
7
by: Venus | last post by:
Hello, I am trying to generate a dynamic form at runtime and would like to do it using "<asp: ..." form elements as follows Build up the string that is placed somewhere in the HTML code the...
4
by: Venus | last post by:
Hello, Thanks for your reply. I understand that a control can be created dynamically in several ways: 1) using StringBuilder 2) using Controls.Add 3) using ASP PlaceHolder But this is just...
0
by: Venus | last post by:
Hello, After trying some ways to do it I wanted to use something like the code below but for some reason is not working (I have to generate the entire form dynamically (not only the controls)):...
2
by: Steve Bottoms | last post by:
Hi, all! Using VB as code-behind in asp.net page... I have a TABLE control which I'm building dynamically. After the table is built, I'm trying to retrieve the HEIGHT property of that table...
10
by: Jen | last post by:
I have a form that has two radio buttons. When the first one is clicked, I would like the page to refresh (keeping the form data in tact) and then displaying 2 new fields that need to be filled...
9
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.