1

I have the following code, the elements are added to the page, but unusually the click event never fires. Any ideas?

function ToggleData(e) {
    var parentRow = $(this).parent().parent();
    var rowData = $(':nth-child(2)', parentRow);
    var html = $("<input type='text' id='amendedval' value='" + $.trim(rowData.text()) + "'/>");
    //Add the update tick
    var imgTick = $("<img src='../images/tick.png' id='imgTick'/>");
    rowData.text('');
    rowData.append(html);
    rowData.append(imgTick);
    imgTick.click(updateTickClick);
};

function updateTickClick(e) {
    alert('hi');
};
4
  • Could you use FireBug to check if rowData and parentRow are actually containing any DOM objects? (maybe the selectors return 0 elements) Commented Jul 2, 2009 at 13:29
  • The img is being added to the page correct with the correctdata. Commented Jul 2, 2009 at 13:33
  • try window.alert('hi') maybe ;-) Commented Jul 2, 2009 at 13:41
  • Interesting if I add the image to parentRow, and not rowData the click event fire's and i'm not sure as to why. Commented Jul 2, 2009 at 14:04

6 Answers 6

2

Can you try

$("imgTick").onclick = updateTickClick;

I'm thinking the adding of an event handler may require an actual DOM reference rather than something built out of innerHTML.

--Edit

Looks like the problem is in your selecting of parentRow and rowData - here's my simplified example which works for me in both FF3.5 and IE7. The only change is how rowData is selected:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Testing onclick</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
    function ToggleData(e){
        var rowData = $('td');
        var html = $("<input type='text' id='amendedval' value='" + $.trim(rowData.text()) +"'/>");
        //Add the update tick
        var imgTick = $("<img src='tick.png' id='imgTick'/>");
        rowData.text('');
        rowData.append(html);
        rowData.append(imgTick);
        imgTick.click(updateTickClick);
    };
    function updateTickClick(e) {
        alert('hi');
    };
    </script>
</head>
<body>
    <table>
        <tr>
            <td><a href="#" onclick="ToggleData(this); return false;">Click me</a></td>
        </tr>
    </table>
</body>
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

Still the same issue no event being fired.
He's using jQuery's click event binder(instead of the onclick DOM one). imgTick should be part of the DOM anyway, since it's appended to rowdata.
How about without the quotes around imgTick? I suggest you check out Firebug (getfirebug.com), its debugger is really useful for eliminating bugs like this.
OK, so 'imgTick = $("<img src='../images/tick.png' id='imgTick'/>");' is equivalent to 'document.createElement("IMG")'? jQuery parses the string and then creates and returns a DOM element rather than inserting it into the document with innerHTML?
1

Your imgTick is being added dynamically to the page. Instead of using .click(), try using .live:

   imgTick.live("click", updateTickClick);

And, no, contrary to what dhaval said, it does NOT have to be anonymous.

2 Comments

that's what I thought but thought I would check.
He is binding the click after the element is added to the page.
0
imgTick.bind("click", function(){
alert ( "Clicked" );
});

Comments

0

Did you try debugging in VS to see whats going on here. The information provided here seems incomplete to deduce anything. For a div with id imgTick, I would do this in jQuery for a click event :

$('#imgTick').click(function({

alert('Hi');

});

1 Comment

I think the selector used for getting the imgTick variable is incorrect? Shouldn't it be just $('#imgTick').
0

Changed var rowData = $(':nth-child(2)',parentRow); To var rowData = $('td:nth-child(2)',parentRow);

Now the click event is occuring correctly.

Comments

0

it can be a callback registered with or without anonymous function

3 Comments

Why does it need to be? I thinks it's perfectly valid to pass a function reference.
An anonymous function is not required, function reference is perfectly legal.
Without live as Tony has pointed out , a function callback is required to register for events whose firing is unknown at start. needs be is changed to can be

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.