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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.