Jump to content

JavaScript - change the actual event called with an element


PC Nerd

Recommended Posts

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.