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

can a function change fields without having "onchange()" in field defintion?

I am working with a system that allow me to add custom fields but I can not
add OnChange() language to the custom fields.

So I want to have a function in the header that recognizes when fieldx is
changed, do a calculation with the value of fieldx and change fieldy.

Can that be done without having to add an OnCHange() event to fieldx?

If so, can you give a quick example or link?

Many thanks.
Jul 2 '08 #1
7 2209
"TriAdmin" <lo*@invalid.comwrites:
I am working with a system that allow me to add custom fields but I can not
add OnChange() language to the custom fields.
Do you mean you can't add an onchange event at all? Note that if you
can insert any javascript code at all in the page, you can look up the
input elements and add onchange handlers at that time:

document.getElementById("some-input-element").onchange = function(e) { ... }.

though finding the elements may be more involved than that if they
don't have predicable IDs.
So I want to have a function in the header that recognizes when fieldx is
changed, do a calculation with the value of fieldx and change fieldy.

Can that be done without having to add an OnCHange() event to fieldx?
You could use setInterval to check the values of the elements, but it
may get hairy. For instance, onchange events only fire for text
inputs on blur (when the focus moves off the input element). You'd
have to emulate that behaviour yourself, and I'm not even sure you can
do that if you can't at least set onfocus and onblur events.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 2 '08 #2
"Joost Diepenmaat" <jo***@zeekat.nlwrote in message
news:87************@zeekat.nl...
"TriAdmin" <lo*@invalid.comwrites:
>I am working with a system that allow me to add custom fields but I can
not
add OnChange() language to the custom fields.

Do you mean you can't add an onchange event at all? Note that if you
can insert any javascript code at all in the page, you can look up the
input elements and add onchange handlers at that time:

document.getElementById("some-input-element").onchange = function(e) {
... }.

though finding the elements may be more involved than that if they
don't have predicable IDs.
>So I want to have a function in the header that recognizes when fieldx is
changed, do a calculation with the value of fieldx and change fieldy.

Can that be done without having to add an OnCHange() event to fieldx?

You could use setInterval to check the values of the elements, but it
may get hairy. For instance, onchange events only fire for text
inputs on blur (when the focus moves off the input element). You'd
have to emulate that behaviour yourself, and I'm not even sure you can
do that if you can't at least set onfocus and onblur events.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Thanks. I CAN add javascript to the page but NOT in the input field. So
can I have this?
<script>
....
document.getElementById("some-input-element").onchange = function(e)
function() {
//update field_Y
}
<script>
Jul 2 '08 #3
"TriAdmin" <lo*@invalid.comwrites:
Thanks. I CAN add javascript to the page but NOT in the input field. So
can I have this?
<script>
...
document.getElementById("some-input-element").onchange = function(e)
function() {
//update field_Y
}
<script>
You'll have to make sure the assignment of the onchange property is
executed after the input element is loaded (and processed by the
browser). lots of times just putting the code at the very end of the
page will work, but to be safe, you'd probably want to use an onload
handler. Something like:

in the page body tag:

<body onload="init_my_stuff()"...

and then define init_my_stuff anywhere, as long as it's immediately
loaded:

<script>
function init_my_stuff() {
document.getElementById("some-input-element").onchange = function(e)
//update field_Y
}
</script>

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 2 '08 #4
The following code is throwing an error when the page loads: Error:
document.getElementById("custom_187") is
nulldocument.getElementById("custom_187").onchange = function(e) {
alert("kjhsdkhsdkh");
}
Sorry, I am not a javascript expert - much appreciate any help!
Jul 2 '08 #5
"TriAdmin" <lo*@invalid.comwrites:
The following code is throwing an error when the page loads: Error:
document.getElementById("custom_187") is
nulldocument.getElementById("custom_187").onchange = function(e) {
alert("kjhsdkhsdkh");
}
Sorry, I am not a javascript expert - much appreciate any help!
you probably don't have an element with id "custom_187", or your code
is executing before the element is loaded and parsed. See my other
reply in this thread. If that doesn't work, it may help to post an URL
for us to check it out.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 2 '08 #6
getting it to work - pretty close now. The code does come before the fields
but should be able to take it from here - many thanks again!
Jul 2 '08 #7
SAM
TriAdmin a écrit :
The following code is throwing an error when the page loads: Error:
document.getElementById("custom_187") is
nulldocument.getElementById("custom_187").onchange = function(e) {
alert("kjhsdkhsdkh");
}
Sorry, I am not a javascript expert - much appreciate any help!

<script type="text/javascript">

function init() {
var chp = document.getElementById("custom_187");
if(!chp) alert('this field doesn\'t exist');
else
{
chp.onchange = function() {
alert('this field\'s value is : '+this.value);
}
}
}

window.onload = init;
</script>

Take care your inputs must have an id

<form blah ... >
<input name="custom_187" id="custom_187" >
<input name="custom_187_b" id="custom_187_b" >
....
</form>


variante :

<html>
<script type="text/javascript">
function updateField(field) {
field.form[field.name+'_tax'].value = field.value * 19.6 / 100;
field.form[field.name+'_total'].value = field.value * 1.196;
}
function init() {
var chp = document.getElementById("custom_187");
if(!chp) alert('this field doesn\'t exist');
else
{
chp.onchange = function() {
updateField(this);
}
}
}
window.onload = init;
</script>
<form>
amount: <input name="custom_187" id="custom_187" >
taxes: <input name="custom_187_tax" id="custom_187_tax" >
total amount: <input name="custom_187_total" id="custom_187_total" >
</form>
</html>

--
sm
Jul 2 '08 #8

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

Similar topics

2
by: Mark Durgee | last post by:
I have a "submit" button in a form that creates a record in my Filemaker database that works as it should. This is the HTML for it: <INPUT TYPE="SUBMIT" name="-New" VALUE="Add Record"> I...
1
by: Ron Brennan | last post by:
I've spent all morning on this and now think there is a (slight?) possiblity that onchange doesn't work for input type="file". Can anybody shed any light on this. The onblur fires just fine. The...
1
by: John Pastrovick | last post by:
Is there a way to load an image locally (in the client) when a selection of a file is made using input type=file. The purpose is to allow selection of a file and put the image in the browser...
5
by: Good Man | last post by:
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...
3
by: Norm via DotNetMonster.com | last post by:
I have an onchange event which fires when the content of an HTML textbox changes. My problem is that when I try to compare the content of this textbox with another HTML textbox using an if...
1
by: julian.tklim | last post by:
Hi, I've got an input box with popup calendar (date picker) all generated using javascipt. Once a date is selected from the datepicker window, the date value is populated back to the input...
6
by: jwarnock | last post by:
When a text field is terminated by a "tab" or change of focus, the browsers behave the same (IE, FireFox, Safari, Netscape). When the text field is terminated by a "return", then "onchange" is...
4
by: mbiasetti | last post by:
Hello, quick question (hopefully): I have a script that manipulates two "Select" elements. My onload calls a function that sets onchange for each "Select": ...
14
by: white lightning | last post by:
How to have <select onchange="this.form.submit()"and also a Submit button on one form? I have something like this: <form action="<?php $_SERVER; ?>" method="post"...
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
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
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
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...
0
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...

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.