HTML Tutorial


 Forum HomeForum Home   FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
RegisterRegister - Not registered yet? Got something to say? Join HTML Code Tutorial!
DOM onChage attribute on select node
Post new topic   Reply to topic    HTML Help Forum Index -> DHTML
View previous topic :: View next topic  
Author Message
k11stan



Joined: 29 Dec 2004
Posts: 1

PostPosted: Wed Dec 29, 2004 9:36 am     DOM onChage attribute on select node Reply with quote

Here's the prob. :

I'm dynamically creating and expandable form through DOM (document.createElement appendElement and the like) . The form takes in a different amount if inputs depending on the user (this part i got figured out) .. as soon as a user clicks add another more input fileds are appended to the form - now here's the kicker - i want to use an onChange event to automatically fill the next field depending on the vaule chosen (this works when the form has a fixed amount of fields ) - for some reason the event gets tripped after the field gets appended and dosent work afterwards ..

thanx for any help ...

here's the code (right now it's still rough - the onChange should display alert "stuff" when the field is clicked and different value is selected , instead it pops the alert after select is appended and the options filled in ... whats up ?)


<html>
<head>
<title>Untitled web-page</title>
<script language="javascript">

//the main form - stuff will get appended to it .. hopefully
var f=document.createElement('form');
var projnum = 0 ;//keeps track of projection fields
var testar = new Array("peps","tov","beef");
var testar1 = new Array ("peps1","peps2","peps3");


function projectionset(){
var pgbreak = document.createElement('br');//pagebreak
f.appendChild(pgbreak);
fieldid=new Array ("a","b","c","d","e","f");
for (i=0;i<=4;i++){
fieldname = projnum + fieldid[i];
field (fieldname);
sfield=document.getElementById(fieldname);
sfield.onChange=fieldevent(fieldname);
}
//textf = document.createElement('input');
//texff.setAttibute('type'
projnum++;

}

function formstart(){
f.setAttribute('name','projection');
var b=document.createElement('input');
b.setAttribute('type','submit');
b.setAttribute('value','click to test this form');
f.appendChild(b);
document.body.appendChild(f);
}

function field(name) {
selectf=document.createElement('select');
selectf.id=name;
selectf.name="test stuff";
soption = document.createElement('option');
selectf.appendChild(soption);
soption.appendChild(document.createTextNode(name));
f.appendChild(selectf);
fillarray(name,testar);
}


//this fills out selects
function fillarray(fieldid,valarray){
selfield=document.getElementById(fieldid);
//1st select
if (fieldid.indexOf("a") != -1){
for (j=0;j<=valarray.length;j++){
selfield.options[j+1] = new Option (valarray[j]);
}

}

}






function fieldevent(fname){

alert (fname);
}


</script>


</head>
<body onload="formstart()">
<a href="#" onClick="projectionset()">another</a>


</body>
</html>
kanenas



Joined: 14 Dec 2004
Posts: 191

PostPosted: Mon Jan 03, 2005 6:06 am     2 bugs Reply with quote

There are 2 problems in function projectionset() when you set the onChange event handler:

  1. 'fieldevent(fieldname)' is evaluated immediately. You need to use (e.g.) an anonymous function to delay evaluation. You may have noticed this already; I bet you wil kick or already have kicked yourself over this one.
  2. assignment to onChange doesn't seem to register the event handler under Gecko or in IE (don't know about other browsers). Use addEventListener and attachEvent or a cross browser library which supports addEventListener instead.


Example:
in projectionset, replace

Code:
sfield.onChange=fieldevent(fieldname);


with
Code:
sfield.onchange = "fieldevent('" + fieldname + "')";
var onChangeHandler = new Function(sfield.onchange);
if (sfield.addEventListener) {
    sfield.addEventListener('change', onChangeHandler, false );
} else if (sfield.attachEvent) {
    sfield.attachEvent('onchange', onChangeHandler);
}


Note 'function () {fieldevent(fieldname);}' won't work for the change event handler, as 'fieldname' in every anonymous function thus generated will be aliased to 'fieldname' in projectionset, which results in every anonymous function having the same value for 'fieldname'.

If you use event properties, don't forget to deal with the different event handler prototypes.
Display posts from previous:   
Post new topic   Reply to topic    HTML Help Forum Index -> DHTML All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
HTML Help Archive
Powered by phpBB © 2001, 2005 phpBB Group
HTML Help topic RSS feed 

 
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   Link Directory   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial