tags:

views:

198

answers:

6

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');
};

Thanks.

+2  A: 

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"&gt;&lt;/script&gt;
    <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>
robertc
Still the same issue no event being fired.
RubbleFord
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.
Robert Massa
How about without the quotes around imgTick? I suggest you check out Firebug (http://getfirebug.com), its debugger is really useful for eliminating bugs like this.
nikc
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?
robertc
A: 
imgTick.bind("click", function(){
alert ( "Clicked" );
});
rahul
A: 

it can be a callback registered with or without anonymous function

dhaval
Why does it *need* to be? I thinks it's perfectly valid to pass a function reference.
Robert Massa
An anonymous function is not required, function reference is perfectly legal.
nikc
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
dhaval
A: 

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');

});

theraneman
I think the selector used for getting the imgTick variable is incorrect? Shouldn't it be just $('#imgTick').
theraneman
+1  A: 

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.

Tony Miller
that's what I thought but thought I would check.
RubbleFord
He is binding the click after the element is added to the page.
Daniel Moura
A: 

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

Now the click event is occuring correctly.

RubbleFord