0

When you specify a function (with or without parameters) in the DOM via onChange, you are limited to static paramters. E.g.

<select id="blah" onChange="doSomething(3);">

However, jQuery lets you dynamically define and assign functions to event handlers, such as 'change'.

See the example below:

function addRow(customObject) {
  << Create TR containing a select named "#someSelect + customObject.id" >>

  // Demo
  $("#someSelect" + customObject.id).change(function() {
    changeSomething(customObject)
  });
}

My question is - essentially - will this work? I am using a specific instance o f an object in my jQuery change event handler. Will that particular instance of customObject (wherever it lies) always be bound to this event handler callback?

customObject is initialized at load, and is one of many. For that particular select box, I always want it to be linked to that object.

New to jQuery. Thanks in advance.

EDIT:

I have several rows of items. Each is represented by an object. I just want to make sure that when someone modifies one of the rows via HTML, that the underlying object (bound to the callback function) is properly updated.

I am looking for something like this, conceptually a mapping between:

Row 1 - customObject1
Row 2 - customObject2
Row 3 - customObject3

... such that if I modify one of the HTML elements (e.g. change the select box) in row 3, that behind the scenes, customObject3 is modified

8
  • It will work. You just need to be sure to write your object's id's (i.e. write all your html) before to run that script. You'll probably need to run that script at the end, not in the header. Commented Jun 18, 2014 at 20:17
  • 1
    "will this work?" Did you try it? Did it work? Commented Jun 18, 2014 at 20:18
  • When you say "in the DOM", it's actually in the HTML which gets converted to a handler in the DOM, and you can reference global variables. They don't need to be static values. Commented Jun 18, 2014 at 20:18
  • if customObject.id equals (for instance) 2 and you have an element with id someSelect2, then yes, it will "work". But I'm not sure it will do what you're expecting, as I can't really make out what exactly it is that you're ultimately trying to accomplish here. Commented Jun 18, 2014 at 20:19
  • "Will that particular instance of customObject (wherever it lies) always be bound to this event handler callback?" The instance itself won't be bound. The function references the variable, so it'll get whatever instance happens to be assigned to that variable when the handler runs. Commented Jun 18, 2014 at 20:21

1 Answer 1

1

What you have will work in case customObject.id is defined before adding the listener and you intent the listener itself to be bound to that specific element without moving it around.

From your question I understand that customObject.id is dynamic as in can change its value and depending on this value, the listener can listen on different element. In this case, you'd have to use a bit more general selector and check the value inside the listener itself:

$('select').on('change', function(e) {
    if(this.id === 'someSelect' + customObject.id)
        return myCallback(e);
});

Edit:

The problem lies in accessing the customObject inside the listener. It uses the current content, not the content it used to have when you added the listener.

Easiest solustion I know is:

var select = $("#someSelect" + customObject.id);
select.customObject = customObject; /* save customObject to the select itself */
select.on('change', function() {
    changeSomething(this.customObject); /* don't use global object but its own */
});
Sign up to request clarification or add additional context in comments.

11 Comments

the ID never changes. It represents the ID of an object. Each row has an underlying object. I just want to make sure that when someone modifies the HTML, that the correct instance of the underlying object is acted upon
Well, if it doesn't change, it isn't dynamic and you can set it like $('#someSelectID'), no?
That isn't what I am worried about
Oh dear, what are you worried about, then?
Well, judging by how you modified your question you haven't even tried it! Because you would find out you had it right from the start. When you bind a listener to an element, it stays bound to that element until you remove it, it can't move itself.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.