Jump to content

[SOLVED] anonymous function woes


roopurt18

Recommended Posts

So I'm using an anonymous function as an event handler and trying to save a value within that function to pass to the real event handler.  The loop in page_onload adds an onclick event to every checkbox button with an id beginning with vis_.  The final event handler is receiving, as it's field argument, the last input on the page, which is an input field with no id of type button.

 

In other words, alert(field.id); in visible_onclick is giving me the id of the save button and not the id of a checkbox.

 

function page_onload(){
  // We need to attach an onclick event handler to each checkbox representing
  // the visible property
  var inputs = document.getElementsByTagName("input");
  if(inputs.length && inputs.length > 0){
    for(var i = 0; i < inputs.length; i++){
      var input = inputs[i];
      if(input.type != "checkbox" || input.id.substr(0,4) != "vis_"){
        continue;
      }
      // Create the event handler
      Events.addEvent(input, "click", function(oEvent){
        var ctrl = input;
        visible_onclick(oEvent, ctrl);
        return;
      });
    }
  }
  return;
}

/**
* visible_onclick
* Called when a visible-type checkbox is clicked
* oEvent - The event object
* field - The associated field
*/
function visible_onclick(oEvent, field){
  var id = "req_" + field.id.substr(4);
  var required = document.getElementById(id);
  alert(oEvent);
  alert(field.id);
  if(!field.checked){
    // Not visible, so can't be required either
    if(required){
      required.checked = false;
      required.disabled = true;
    }
  }else{
    // Visible so we have to enable the required checkbox
    if(required){
      required.disabled = false;
    }
  }
  return;
}

 

I r confused!

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.