Solved

Why is my onclick event on row <tr> throws "is not defined exception in HTML5/jquery?

Posted on 2013-06-15
5
629 Views
Last Modified: 2013-06-16
Hi:

I'm trying to get the onclick event working in a <tr>

I create <tr>s and add the to my table in javascript.

function LoadTheCodeOnDemand(){

    function ClickGeneratedOrderRow(orderRow){
        alert("Clicked on Row");
    }; 

    function AddARow(){

       tableRow = $('<tr onclick="ClickGeneratedOrderRow(this);"/>');
       // Add a bunch of <td>s to the row
       
       $('#GeneratedOrdersTable > tbody').append(tableRow);

    };

};

Open in new window


When I click on the row at runtime, I get the following error.
(function() {with (this[2]) {with (this[1]) {with (this[0]) {return function(event) {ClickGeneratedOrderRow(this);

Uncaught Reference Error: ClickGeneratedOrderRow is not defined.

Open in new window


Both of the above functions are defined with the same JS file, so scope should not be an issue (I think).

Any ideas would be helpful here. I think I need to pass the "this" so that I'll be able to tell which row was actually clicked. I can then get the required data from the row.

Any advice/thoughts on this?
0
Comment
Question by:jxbma
5 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39250959
that is because u declared ClickGeneratedOrderRow inside LoadTheCodeOnDemand.
try declare it outside this scope, something like:
 
   function ClickGeneratedOrderRow(orderRow){
        alert("Clicked on Row");
    }

function LoadTheCodeOnDemand(){

    function AddARow(){

       tableRow = $('<tr onclick="ClickGeneratedOrderRow(this);"/>');
       // Add a bunch of <td>s to the row
       
       $('#GeneratedOrdersTable > tbody').append(tableRow);

    };

};

Open in new window


btw, why do u declare AddARow() inside LoadTheCodeOnDemand()?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39251175
Should work too :
function LoadTheCodeOnDemand(){

    function AddARow(){

       tableRow = $('<tr onclick="ClickGeneratedOrderRow(this);"/>')
       // Add a bunch of <td>s to the row
       
       tableRow.appendTo('#GeneratedOrdersTable > tbody').click(function() {
               alert("Clicked on Row");
       });

    };

};

Open in new window

0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39251183
Segdwick should have set the point: each function must be declared separately inside the script definition.
0
 
LVL 1

Author Comment

by:jxbma
ID: 39251610
We are integrated with Spring. Declaring functions within a DemandLoadPage() function allows me to package functions in a separate JS file which then get loaded only when the requested page is loaded into the browser.

 (I didn't design the overall architecture here, so I'm not in a position to change it either).

JB
0
 
LVL 1

Author Closing Comment

by:jxbma
ID: 39251683
This works.
I created an Object outside of the scope of the demand load function.
I assigned the function as "a method" on that object.
I then was able to call it via "ScopeObject.ClickGeneratedOrderRow(this)".

All is well,

Thanks,
JB
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now