PC Nerd Posted March 23, 2008 Share Posted March 23, 2008 Hi, Ive got a problem where if my AJAX only places one item in teh select list.. the onChange event doesnt work. becaus of this i want to have code similar to: if(array.length==1) { change the event called} i was thinking: selectobj.onChange=null; selectobj.onClick=setNextField(); from there i would do the same with each change so if an element decided it was longer that 1 element, it would change again. Is that possible, or is there a better way of doing it? Thanks for your help in advance. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 24, 2008 Share Posted March 24, 2008 That should work, but you could run into problems due to differences in browsers. Especially if you ever need to add another event to one of those triggers. You would be wiping out all the trigger events by setting it to null. Instead I would suggest creating a single function to handle boththe onchange and the onclick events. That function would first determine if there is a single element or multiple and then act accordingly. I'm not 100% sure and too lazy to check at the moment - but does a select field even support "onclick"? May need to use "onfocus" function updateSelect (selectObj, trigger) { if ((selectObj.length>1 && trigger=='change') || (selectObj.length==1 && trigger=='focus')) //run the function } } <select name="name" onchange="updateSelect(this,'change');" onfocus="updateSelect(this,'focus');"> However, if hte field only has one value you could just fire the event automatically when loaded instead of forcing the user to click on the field. Quote Link to comment Share on other sites More sharing options...
PC Nerd Posted March 25, 2008 Author Share Posted March 25, 2008 ok, thanks Ive solved it by ensureing that the first list element is " --> " or " Please Select"... that way the onChaneg is always true. Thanks for the suggestion Ill be sure to try itout an play with it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.