4

Morning people. How to make my javascript or jquery works in dynamically generated content.

Basically, i have created web page that generates contents, base on what user clicks on the navigation menu.

The problems i am facing:

  1. when main page generate contents from content-page, jquery or javascript won't work.
  2. but when i open up the content-page alone, everything works.

Information collected through searching:

jQuery.load() method ignores the script tag that comes together with the that content generated dynamically.

So i try the following:

  1. Put the tag that i need in main page, not in content-page. it doesn't work. seem like the jquery can't find the content element, because they are dynamically generated.
  2. Since jQuery.load() ignores script tag, I tried pure javascript ajax like how w3schools.com teaches, the xmlhttp way, to generate the content. It doesn't work.
  3. When a button is clicked. no response in console.

Example contact.php

<script type="text/javascript">
$(function() {
    $("#submitUser").click(function(e) {
    var fname = $("#fname").val();
    $("#theresult").text(fname);
    e.preventDefault();
});
});
<form id="contactForm"> 
<label for='fname' >First Name</label><br/>
<input id="fname" type="text" name="fname" maxlength="100" />
</form>
<div id="theresult"></div>

When this contact.php is dynamically generated into other page, it doesn't work. When I click "submit" button, console shows no response. seem like the jquery is not exists.
But when I open it alone in browser, it works.

Loonb
  • 1,068
  • 5
  • 18
  • 30
  • 2
    Restate your question. What exactly doesn't work? What do you do and what do you expect to happen? Have you checked error console in your browser? Which errors are reported? – Nikola Radosavljević Aug 18 '12 at 20:35
  • 1
    My advice is to make a very small test page that demonstrates the problem, so you can post the code. You might even find the solution by doing that. You definitely *can* use javascript/jquery returned from ajax... I did it yesterday. Have you tried using jQuery's .html() method to insert the content? – AlexMA Aug 18 '12 at 20:36
  • @AlexMA .html() to insert the whole thing and the dynamically generated jquery will work? i haven't try. imma go try now – Loonb Aug 19 '12 at 00:19

4 Answers4

12

For dynamically generated elements you should delegate the events, you can use the on method:

$(function() {
    $(document).on('click', '#submitUser', function(e) {
       var fname = $("#fname").val();
       $("#theresult").text(fname);
       e.preventDefault();
    });
});

live() method is deprecated.

Ram
  • 143,282
  • 16
  • 168
  • 197
  • thanks @undefined. this works fine. i am thinking of reducing the .js file size for the first time page loads. so i want this statement to be generated dynamically when user navigate to this page. is this possible? jquery seems like doesn't exist in the dynamically generated content. thanks XD – Loonb Aug 19 '12 at 00:11
  • 1
    @TianLoon you are welcome, you can use the jQuery `$.getScript()` utility function, however these 4 lines do not help you to minify the size of your js file, you can post your code on `codereview.stackexchange.com` for minifying, there are nice guys there that can help you. – Ram Aug 19 '12 at 00:15
  • thanks again @undefined. i din know there is $.getScript() and the community codereview.stackexchange.com. i'll google about .getScript() and learn how it works. i come back if i have prob with .getScript(). btw, above 4 lines are just example of problem. they are actually very long. XD. but still few KB in size. doesn't matter. i wanna learn this for future use. XD – Loonb Aug 19 '12 at 00:24
  • hell man. the .on() and .getScript() are what i am looking for since 4 days ago! thank you so much! – Loonb Aug 19 '12 at 00:47
  • Wow, this explains so much about my current problems. additionally, Will placing my event handling inside a document ready statement properly delegate things? – Gokigooooks Apr 24 '15 at 12:13
2

Taking a wild stab in the dark here since your question isn't very well-described, but perhaps you're trying to use .click() and so on to bind events to things that are getting dynamically loaded into the page?

If so, you probably want to look at .on() instead.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • 4
    Use `on` instead of `live` which is not support anymore. – maxisam Aug 18 '12 at 20:41
  • 1
    As of jQuery 1.7 `.on()` is the preferred mthod for all event handlers: http://api.jquery.com/on/ – tptcat Aug 18 '12 at 20:41
  • @maxisam beat me to it by a few seconds. – tptcat Aug 18 '12 at 20:42
  • @amber. you get it right. this is what i want to achieve. thanks. .on() actually works. but i still have one question. >> i am thinking of reducing the .js file size for the first time page loads. so i want this statement to be generated dynamically when user navigate to this page. is this possible? jquery seems like doesn't exist in the dynamically generated content. thanks XD – Loonb Aug 19 '12 at 00:16
  • and +1 for everyone that previously stated .live() is deprecated. you guys make me 200% will not use .live(). use .on() XD. and i love this world so much, so many helpful people in this world! – Loonb Aug 19 '12 at 00:17
  • @TianLoon see http://stackoverflow.com/questions/3857874/how-to-dynamically-insert-a-script-tag-via-jquery-after-page-load for details on dynamically loading scripts. – Amber Aug 19 '12 at 02:27
1

You need to use live event.

As an example, if your generated content contain a click event, you could do this:

$(".something").live("click", function({ 
// do something
)};
Didats Triadi
  • 1,502
  • 2
  • 14
  • 13
  • 4
    `live()` method is deprecated. – Ram Aug 18 '12 at 20:54
  • Hey. This works! but only if i put $(".something").live("click", function({ in the main page. what about in content page? i mean as a generated jquery script – Loonb Aug 18 '12 at 21:09
1

For dynamically generated fields use .on() JQuery method:

$(document).on('click', '#MyID', function(e) {
    e.preventDefault(); // or you can use "return false;"
});
Furkat U.
  • 431
  • 5
  • 20