By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,630 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
"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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.