473,474 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamically Changing the 'onchange' value

Hi there

I'm adding form fields on the fly with some javascript DOM programming.
I basically just clone a hidden <div>, then adjust node properties to
make this new <div> have unique values (style, size, etc.)

the original code of the input tag is:
<input type="file" onchange="alert('original')" size="30" id="filea"
name="filea">

One thing I'd like to do is to the onChange event value. I use the
following code to assign my new 'file' input field a new id and name:

newField[i].id=nameFile+newNum;
newField[i].name=nameFile+newNum;

Basically, this changes my form field id and name from 'filea' to
'filea1'. Works fine. I'd like to alter the 'onchange' value in this
same block of code.

Using the Firefox DOM Inspector, I can see that my file input field has
the following properties: type,style,size,name,id,onchange. I can easily
change this field's size to 5 (originally it was 35) by adding:

newField[i].size = '5'

and I can see that it works - my new file input field is 5 characters. If
I look at the field in the DOM Inspector, indeed I see that the nodeValue
of the nodeName "size" is 5.

apparantly I am wrong in assuming that I could change the field's
onChange value in a similar way! doing the following does nothing - it
doesn't change the "onchange" node value (or the alert message) at all.

newField[i].onchange = 'alert(\'changed\')';

to repeat, looking at my DOM Inspector after this script runs, the
nodeValue of the nodeName "onchange" has not changed, even though the
"size" node does change with similar syntax.

I get an interesting value of the onChange element via:
alert(newField[i].onchange). It gives me:

function onchange(event) {
alert("original");
}

Very interesting... not what I was expecting.

How can I dynamically change the 'onchange' value of the file input
field?

Hope I haven't lost anyone!! Much thanks.

Sep 8 '05 #1
5 14516

"Good Man" <he***@letsgo.com> wrote in message
news:Xn***********************@216.196.97.131...
Hi there

I'm adding form fields on the fly with some javascript DOM programming.
I basically just clone a hidden <div>, then adjust node properties to
make this new <div> have unique values (style, size, etc.)

the original code of the input tag is:
<input type="file" onchange="alert('original')" size="30" id="filea"
name="filea">


try something like

var clicked = false

function clicked(){
clicked = true
alert (clicked);
}

<input type="file" onchange="javascript:clicked();" size="30" id="filea"
name="filea">
Sep 8 '05 #2
"Zoe Brown" <zo***********@N-O-S-P-A-A-Mtesco.net> wrote in
news:0Z******************@newsfe1-gui.ntli.net:

try something like

var clicked = false

function clicked(){
clicked = true
alert (clicked);
}

<input type="file" onchange="javascript:clicked();" size="30" id="filea"
name="filea">


hmmm, not really what i was looking for. I want to change the value
itself: in your case, i would want to change "javascript:clicked();" to
something ENTIRELY different, like "javascript:alert('hi mom')" or
"javascript:myownfunction(yeah)".
Sep 8 '05 #3

"Good Man" <he***@letsgo.com> wrote in message
news:Xn************************@216.196.97.131...
"Zoe Brown" <zo***********@N-O-S-P-A-A-Mtesco.net> wrote in
news:0Z******************@newsfe1-gui.ntli.net:

try something like

var clicked = false

function clicked(){
clicked = true
alert (clicked);
}

<input type="file" onchange="javascript:clicked();" size="30" id="filea"
name="filea">


hmmm, not really what i was looking for. I want to change the value
itself: in your case, i would want to change "javascript:clicked();" to
something ENTIRELY different, like "javascript:alert('hi mom')" or
"javascript:myownfunction(yeah)".


well you can do that with the solution above, you are using a variable to
whoch you can assign the required behaviour.

ok bear with me, I am out of practise.

onchange.value = whatever
Sep 8 '05 #4
Good Man a écrit :
apparantly I am wrong in assuming that I could change the field's
onChange value in a similar way! doing the following does nothing - it
doesn't change the "onchange" node value (or the alert message) at all.

newField[i].onchange = 'alert(\'changed\')';
newField[i].onchange = new Function ("evt", "alert('changed!');");


to repeat, looking at my DOM Inspector after this script runs, the
nodeValue of the nodeName "onchange" has not changed,
Try the code:

newField[i].onchange = new Function ("evt", "alert('changed!');");

How can I dynamically change the 'onchange' value of the file input
field?

Gérard
--
remove blah to email me
Sep 8 '05 #5
Good Man wrote:
"Zoe Brown" <zo***********@N-O-S-P-A-A-Mtesco.net> wrote in
news:0Z******************@newsfe1-gui.ntli.net:
try something like

var clicked = false

function clicked(){
clicked = true
alert (clicked);
}

<input type="file" onchange="javascript:clicked();" size="30" id="filea"
name="filea">

There is no need for the javascript pseudo protocol here (it may be
needed if you've specified some other scripting language elsewhere but
that is extremely rare).


hmmm, not really what i was looking for. I want to change the value
itself: in your case, i would want to change "javascript:clicked();" to
something ENTIRELY different, like "javascript:alert('hi mom')" or
"javascript:myownfunction(yeah)".


Use the method suggested by Gérard, or:

<p>Click 'Original' to see the initial onclick. Then click
'Change onclick' to change it, click 'New onclick' to see the
new one.</p>
<input type="button" value="Original" id="but2" onclick="
alert('This is the original onclick');
">
<input type="button" value="Change onclick" onclick="
var but = document.getElementById('but2');
but.onclick = function() {
alert('My id is ' + this.id + '\nThis is the new onclick');
}
but.value = 'New onclick';
">
--
Rob
Sep 9 '05 #6

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

Similar topics

2
by: RelaxoRy | last post by:
I have 3 texfield boxes 1. firstname 2. lastname 3. username When a person enters in their firstname and lastname, I want the username field to fill with firstnameLastinitial. Eg. ...
4
by: Bart van Deenen | last post by:
Hi all I have a script where I dynamically create multiple inputs and selects from a script. The inputs and selects must have an associated onchange handler. I have the script working fine on...
7
by: juglesh | last post by:
Hello, I would like to be able to have the user sort a list of items similarly to the way you sort your queue on Netflix.com. (the numbers dont change dynamically on netflix, they must be doing...
4
by: Stone Chen | last post by:
Hello, I have form that uses javascript createElement to add additional input fields to it. However, my validating script will not process new input fields because it can only find the named...
2
by: somaskarthic | last post by:
Hi In my php code , i dynamically created table rows on button click event. Each row contain 3 selectboxes, 7 checkboxes . On each click of these elements i need to submit the form and save the...
3
by: Greg Scharlemann | last post by:
I'm not sure the best way to accomplish this... my hunch is with javascript, but I'm not sure if using server side code (PHP) would be easier. I'm adding people to a database. People have a...
5
by: J | last post by:
I am having problems dynamically adding more than one event handler to an input. I have tried the Javascript included at the bottom. The lines inp.attachEvent('onkeyup',...
6
by: tuxedo | last post by:
I have a fixed html structure, where only one form and a simple select menu will exist on an html page, as follows: <form action="order" method="POST"> <select name="dinner"> <option...
21
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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: 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 ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.