views:

218

answers:

4

Hello.

I have an ASP.net MVC2 application. In wich I'm using JQuery to alter all table rows so I can click anywhere in any row to trigger a click event on a link in the clicked row.

The tables is created using MVC's built in partialview ajax.

Here is my JQuery script.

<script type="text/javascript">

        $(document).ready(function () {
            $('table tr').live('click',function (event) {
                $('#asplink', this).trigger('click');
            })
            .live('mouseenter',function (event) {
                this.bgColor = 'lightblue';                    
            })
            .live('mouseleave', function (event) {
                this.bgColor = 'white';
            });

        });

</script>

And this is the first part of the partial view code that creates the table.

<% foreach (var item in Model.JobHeaderData)
   { %>

    <tr>
        <td>
            <a id="asplink" href="http://localhost/sagstyring/EditJob.asp?JobDataID=&lt;%: item.JobDataId %>&JobNumId=<%: item.JobNumID%>&JobNum=<%: item.JobNumID%>&DepId=1&User_Id=<%:ViewData["UserId"]%>" onclick="window.open(this.href,'Rediger sag <%: item.JobNumID %> ', 'status=0, toolbar=0, location=0, menubar=0, directories=0, resizeable=0, scrollbars=0, width=900, height=700'); return false;">Rediger</a>                 
        </td>

In firefox this works perfectly. In IE, JQuery crashes when I click on a row.

If I debug the page in IE. I get this.

Out of stack space

In jquery-1.4.1.js line 1822

 // Trigger the event, it is assumed that "handle" is a function
    var handle = jQuery.data( elem, "handle" );
    if ( handle ) {
        handle.apply( elem, data );
    }

I'm no eagle at javascript, so I'm pretty much stuck.

Edit: IE had a problem with spaces in my window.open function on the click event. Having fixed that, I can now see that the click event is actualy working, but it enters a Loop. I just keeps clicking on the link until I get the Out of stack space error.

Any thoughts on this?

A: 

try:

$('table tr').live('click',function (event) {
  $(this).find('#asplink').trigger('click');

})

scratch that.

looking at your code again, i really think it's the fact that all the rows have the same id. try giving them all theclass aslpink instead of the id. and change the jquery to:

$('table tr').live('click',function (event) {
  $(this).find('.asplink').trigger('click');

})

hmmmm ok: try giving the rows all a unique id. something like id='#asplink<%=JobId%>' and use

$(this).find('.asplink').click();

is there other tables on the page?

Patricia
Same error. Diffenrent place in JQuery it fails. Line 1593.if ( !handle ) { eventHandle = function() { // Handle the second event of a trigger and when // an event is called after a page has unloaded return typeof jQuery !== "undefined" }; handle = jQuery.data( elem, "handle", eventHandle ); }
olve
out of curiosity: what happens if you just call .click() instead of .trigger('click')also: i just noticed: #asplink is a selector for everything with the ID asplink. do you have more then one thing on your page with the same Id? this might be what's throwing IE off.
Patricia
Just tried with .click instead of trigger. It still works in FF, but in IE I get an "Object doesn't support this property or method", but the click event is still executed. Every row in my table will have a link with the same id. If I change it into a class, I get an out of memory error instead.
olve
"Every row in my table will have a link with the same id." This is a violation of the HTML specification, which says that every id on a page must be unique. I suggest making that a class instead.
R. Bemrose
A: 

If you still have the asplink class on your anchor tags try adding this code to your document on ready:

$('a.asplink').live('click', function(event) {
    event.stopPropogation();
}

This should prevent the click event on the anchor tag from bubbling up to the td tag and triggering the click event on the anchor tag again.

patrickmcgraw
This code have the same effect as calling click instead of trigger('click'). I get an "Message: Object doesn't support this property or method"Line: 37Char: 21This is the event.stop.... line
olve
A: 

Try this:

$('table tr').live('click',function (event) {
    $('#asplink', this).trigger('click');
    return false;
})

Based on this question, it looks like doing event.stopPropogation(); won't work with live and you'll need to use return false;. I'm not sure why this would be a problem in IE but not Firefox though.

rosscj2533
Doesn't work either. I've tried it with both class and id reference. It still enters a loop. The link is opened several times until the main page throws a "Stack overflow"
olve
A: 

Found a solution.

This works.

$('table tr').live('click', function (event) {                    
                window.open(jQuery(this).find(".asplink").attr('href'), 'Edit', 'status=0, toolbar=0, location=0, menubar=0, width=900, height=700');                               
            })
olve