0

I have a main view calls Index which contains some textboxes and allow additional textboxes to be added to the screen via partial view calls. The main view reference a js file which has the following code:

External JS file:

$("input[type='text']").on("change", function () {
 alert("here");
});

The code works whenever I modify a value in a textbox loaded by the main view. However, it does nothing when I attempted to change the value from a textbox that is added by partial view.

Per my understanding, the live() method should handle the current element in the dom and any future element added to the dom. This is now being replaced with .on(). Hence, I'm using .on(). Is there anything new that is replacing .on() that I don't know of? .live() and .on() don't work in my case.

For good practices, I don't want to have the js/jquery code added to the partial view again to make it works. I'm hoping to avoid redundancy and implement good practices. Any help is appreciated.

5
  • look into delegate() stackoverflow.com/questions/4579117/jquery-live-vs-delegate Commented Jan 8, 2015 at 22:01
  • How are you loading the partial view? Is it through Ajax? Commented Jan 8, 2015 at 22:06
  • yes. I think we figured it out with the answers below. Commented Jan 8, 2015 at 22:12
  • 2
    I think if you are concerned with right way, you should consider event bubble up. If elements are always added in the specific container, better write delegation based on that specific container context because $(body) or $(document) will bubble up all the way up... Commented Jan 8, 2015 at 22:15
  • @KrishnaDhungana +1 for a valid point. Thank you. Commented Jan 8, 2015 at 22:31

4 Answers 4

2

The on() has to be bound to an element that already exists in the DOM when the page is loaded to delegate the event to the element that was added dynamically:

$(document).on("change", $("input[type='text']"), function () {
  alert("here");
});

Instead of $(document) any static parent element can be used to delegate the event.

Sign up to request clarification or add additional context in comments.

Comments

1

Try to use following code:

$(document).on("change", "input[type='text']", function () {
 alert("here");
});

document can be changed on any parent node for both inputs

1 Comment

thank you it works! I didn't know this. Can I possibly accept two of the same answer? you and matthias_h answered about the same time.
1

You should use event delegation. Put all of your inputs inside a container div and put the event on this parent element. It will work for dom elements added dynamically. So if you put them in:

<div class="container">
  <input ...>
  <input ...>
  <input ...>
</div>

the jQuery would be:

$('.container').on('change','input[type="text"]', function(e) {
  var el =e.target;  //if you need the element clicked on
  alert('here');
});

Comments

0

So, basically you are trying to delegate the event. To do so, as per jQuery documentation says, you need to use something like this:

```

$('body').on('change', 'input[type="text"]', function(e) { 
  console.log(e.target); // the input that triggered the event
  console.log(e.currentTarget); // the body element
});

```

Listening for events in the body might be too much, you may want to listen 'change' events within some specific form or element.

Comments

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.