0

Given this fiddle, what is preventing the HTML from being rendered?

JavaScript

$(document).ready(function(){
    function loadDiv( divId ) {
        $('#' + divId).html('<table><tr><td class="editable" id="' + divId + '">Edit Me</td></tr></table>');
    }

    $('td.editable').click(function() {
        var cellId = $('td.editable').attr('id');
        alert(cellId);
    });

    loadDiv( div1 );
    loadDiv( div2 );
});

My intention is to change the clicked cell into an input field and later post from it, but I'm not sure why it's not rendering.

1
  • div1 and div2 two are variables... put quotes around them. Commented Feb 25, 2013 at 20:42

1 Answer 1

3

You need to add quotes around your div ids.

loadDiv( "div1" );
loadDiv( "div2" );

http://jsfiddle.net/pp6nv/1/

Since the tables are added after the page loads, you'll have to use .on().

$('body').on("click", "td.editable", function() {
    var cellId = $(this).prop('id');
    alert(cellId);
});

http://jsfiddle.net/pp6nv/3/

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

1 Comment

Sigh. Thanks for that. What must happen for it to be able to get caught by the click function?

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.