4

I'm trying to live edit data. To do this I found this script

http://www.9lessons.info/2011/03/live-table-edit-with-jquery-and-ajax.html

The script works perfectly when the table get's created in the php file but I'm trying to do this by injecting the table through javascript.

This is my php page

<script type="text/javascript" src="javascript/default/gebruikers.js"></script>
<div id="adminGebruikersDiv" class="adminGebruikersDiv"></div>
<script type="text/javascript">
    onload = function() { loadGebruikers(); };
</script>

the function loadGebruikers creates the table and injects it in the div. Now I want this code to work

$j(document).ready(function() {
    $j(".rij").click(function() {}).change(function() {});
    $j(".editbox").mouseup(function() {});
$j(document).mouseup(function() {});
});

So how can I do this using jquery

EDIT: now kind of works

$j(document).on("ready", function() {
    $j(document).on("click", ".rij", function() {}).on("change", function() {});
    $j(document).on("mouseup", ".editbox", function() {});
    $j(document).on("mouseup", ".document", function() {});
});
eric22269
  • 65
  • 1
  • 8
  • 1
    *is this the right way to use .on?* There's no `.on()` anywhere there. – JJJ Feb 01 '13 at 12:38
  • 1
    Just take a look at example in my answer. – Aleksandr M Feb 01 '13 at 12:42
  • 1
    Please have a look at the [**on() documentation**](http://api.jquery.com/on/) You need to specify the event first, then the element. Also, please do **not** post code which has incorrect syntax. You are missing a closing brace `);` in your original code. How are we to know that this is not the reason it doesn't work? you can use tools like [**jsfiddle**](http://jsfiddle.net/) to help you out with the formatting of your code and it's syntax correctness. – Nope Feb 01 '13 at 12:45

1 Answers1

3

You should use .on to attach events to dynamic elements.

$j(document).on("click", ".rij", function() { ... });
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • Or .live if you're using an older version of jQuery – Jeff Paquette Feb 01 '13 at 12:29
  • I'm using jquery 1.9 so .live doesn't work. .on doesn't work either but maybe i'm doing it wrong $j(document).ready(function() { $j(".rij").click(function() { }).change(function() { }); $j(".editbox").mouseup(function() { } $j(document).mouseup(function() { }); }); – eric22269 Feb 01 '13 at 12:34
  • 1
    @eric22269: But you are not using `.on`. – Aleksandr M Feb 01 '13 at 12:37
  • Pasted the wrong code, first post now shows the code with .on – eric22269 Feb 01 '13 at 12:43
  • 1
    @eric22269: I think you will have to go through the `on` documentation I linked in the comments on your question. By all means I'm not saying read the manual for your answer as we are always happy to help out but if you are missing the basic essentials to understand an answer (not ment to be insulting) you might need to have a read over it quickly. – Nope Feb 01 '13 at 12:48
  • You are right, I'm reading the documentatoin right now and am one step further. – eric22269 Feb 01 '13 at 12:51
  • 1
    @ Aleksandr M, alright thanks for the help. .on works although there's still something wrong with my code. I'm gone read some documentation and see if I can figure it out. – eric22269 Feb 01 '13 at 13:09