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
https://forums.phpfreaks.com/topic/48693-solved-anonymous-function-woes/
Share on other sites

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.