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

T

TriAdmin

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.
 
J

Joost Diepenmaat

TriAdmin said:
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.
 
T

TriAdmin

Joost Diepenmaat said:
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.


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.

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>
 
J

Joost Diepenmaat

TriAdmin said:
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>
 
T

TriAdmin

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!
 
J

Joost Diepenmaat

TriAdmin said:
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.
 
T

TriAdmin

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!
 
S

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top