0

UPDATE: For the people of the f u t u r e, it wasn't a syntax problem. As I originally suspected, there was a problem loading the html template properly and the checkbox update was running before it had finished. So if it seems as though the element you're looking for isn't there, well that just might be the case. AsyncJS

ORIGINAL: Okay, I thought I was better than this, but I've done everything I can. I've got a simple task manager webapp. I want to set the 'checked' attribute of a checkbox input field to 'true' when the status property of my task object is 'true' after a successful get request.

Before I do this however, I inject my dynamic html using the Mustache.js template engine by calling a helper function. I created a callback function that will execute once the html is injected to avoid searching for nothing when I go to mark 'checked' to 'true'. That all looks like this:

DOM:

html

// AJAX call for getting current tasks
$.ajax({
    url: '/tasks',
    type: 'GET'
}).done(function(response, textStatus, jqXHR){
    $.each($.parseJSON(response), function(i, task){
        insertTask(task, function(){
            if (task.status == true){
                console.log("Task with status of 'true': ");
                console.log(task);
                console.log("Below should be the checkbox");
                console.log($('body').find($('#' + task.id)));
                $('#tasks').find($('#' + task.id)).prop('checked', true);
            }
        });
    });

Template handler:

    // Uses the mustache template engine to dynamically insert tasks into DOM
function insertTask(data, callback){
    $.Mustache.load('templates/tasks.html', function() {
        $('#tasks').mustache('main_form', data);
    });
    if (callback && typeof(callback) == 'function'){
        callback();
    }
}

Output: output

When I do console.log($('#tasks').find('#' + task.id)); all I get back is the #tasks object. Even if I just search for an 'li' it returns the #task object. So obviously this sounds like the html just hasn't been injected yet. But I don't see how that's possible given that I have the callback function.

Maksym Semenykhin
  • 1,924
  • 15
  • 26
GHOST-34
  • 359
  • 1
  • 19
  • you're not using selector in find function,Try this: $('#tasks').find($('#' + task.id)).prop('checked', true); – Shilpa Soni Jun 24 '16 at 06:16
  • 1
    I'm not sure if that selector is necessary or not, but it didn't change the results regardless. As mentioned in the post, even searching for an 'li' element (and I've checked other types) came up that same so it feels less of a syntax problem. – GHOST-34 Jun 24 '16 at 06:37

2 Answers2

2

The id of your checkbox is like 2a49....

Your selector is $('#tasks').find($('#task_' + task.id)).prop('checked', true);

It compiles to

>>$('#tasks #task_2a49....

which won't return anything as it tries to find element with id = task_2a49.....


Just use,

$('#tasks').find('#'+task.id) 

Put your call to callback() inside the .load(). The callback was being triggered before load() returned results.

$.Mustache.load('templates/tasks.html', function() {
    $('#tasks').mustache('main_form', data);

    if (callback && typeof(callback) == 'function'){
        callback();
    }
});
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
  • It was actually that way before Семенихин Максим mentioned possible restrictions. I have since updated the DOM to prefix the id with "task_" hence why you see it in the js. I'll change it back since starting with a number IS actually okay, but I'm still not getting any positive results. – GHOST-34 Jun 24 '16 at 06:58
  • Nice, but still nothing. To reiterate, not even searching for an 'li' is coming up properly. I could add a class called 'test' and search for it in the html template and it won't come up. So I really don't think this is a syntax problem. – GHOST-34 Jun 24 '16 at 07:06
  • 1
    Booyah! Nice catch. I figured that's where the problem was occurring. Didn't realize callback was being executed first. Async JS is a tricky thing! – GHOST-34 Jun 24 '16 at 07:47
  • True! I skipped debugging `Mustache` code thinking the problem might be in the selectors. – Shaunak D Jun 24 '16 at 08:44
1

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

link to more info

Community
  • 1
  • 1
Maksym Semenykhin
  • 1,924
  • 15
  • 26
  • That is for html4, not necessary in html5. - **[Fiddle example](https://jsfiddle.net/oL24k2Lt/6/)** – Shaunak D Jun 24 '16 at 06:21
  • 1
    use link from answer. HTML 5 is even more permissive, saying only that an id must contain at least one character and may not contain any space characters. – Maksym Semenykhin Jun 24 '16 at 06:22
  • I did use link from the answer. *ID and NAME tokens must begin with a letter ([A-Za-z])* is for html4 and not htlm5. html5 allows ID like `2adffgf-3ghfggf` – Shaunak D Jun 24 '16 at 06:30
  • I think it was a good catch. It wasn't something I was aware of. Regardless, it didn't help. I've updated my answer with some output. – GHOST-34 Jun 24 '16 at 06:33