Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:20:18+00:00 2026-06-09T14:20:18+00:00

i have problem with my table that generate from function. i need to get

  • 0

i have problem with my table that generate from function.

i need to get the clicked td attr and run some function.
but it doesn’t work.
what wrong with my code..?

generate_table.js

function generate_table(data,lnk){
    tbl='';
    table_stayle=' border=1; cellpadding=0; cellspacing=0;' ;
    th_stayle='style="color:#FFFFFF; font-weight:bolder; background-color:#003366" ';
    tr_stayle=' bgcolor="#CCCCFF" ';
    td_stayle='';
    tr_alternate='';
        var tbl_body = '';
        var tbl_h = '';
            $.each(data[0], function(k , v) {
                tbl_h=tbl_h + "<td>"+k+"</td>";
                })
        tbl_h ='<tr align="center" '+th_stayle+'>'+tbl_h+ '</tr>';
            //detail
        var tbl_body = "";
        $.each(data, function() {
            var tbl_row = "";
            var row_key='';
                $.each(this, function(k , v) {
                    if(k==lnk){
                        row_key=v;
                        tbl_row =tbl_row + "<td><a href='javascript:void(0);'>"+v+"</a></td>";//<a href='"+v+"'>
                    }else{
                        tbl_row =tbl_row + "<td>"+v+"</td>";
                    }
                })
            tbl_body =tbl_body+ '<tr '+tr_stayle+' kode="'+row_key+'" class="tbl_tr" >'+tbl_row + '</tr>';               
        })
        return '<table id="tabel" '+table_stayle+'>'+tbl_h+tbl_body+'</table>';
}

php code like this :

    <script language="javascript1.5" src="jquery-1.7.2.min.js"></script>
<script language="javascript1.5" src="generate_tabel.js"></script>
<style>
#div_body {overflow:auto; height:300px;}
</style>
======================================================================<br />
<input type="button" id="btn" name="btn" value="load data">
<div id="div_body" style="overflow:auto;border:solid; border-width:medium; width:400;300">    
</div>
<script>
$(function(){
    $('#btn').click(function(){
        url="getjson.php";
        aa=$.getJSON(url , function(data) {
    //JSON DATA LIKE THIS:
    //          aa='[{"Id":"1","wh2":"HandPhone","wh3":"-"},
    //          {"Id":"2","wh2":"Acessory","wh3":"-"},
    //          {"Id":"3","wh2":"Batery","wh3":"-"},
    //          {"Id":"4","wh2":"Chassing","wh3":"-"}]';
            $('#div_body').html(generate_table(data,"wh2"));
            });
        });

    //  $(".tbl_row").click(function(){
    //        alert($("tr").attr("kode"));
    //        //some function
    //     });
    $('#div_body').on('click','.tbl_tr',function(){ alert($(this).attr("kode")); });
});
</script>

and the function handler click :

$(".tbl_tr").click(function(){
          alert($("tr").attr("kode"));
          //some function
       });

yes.. mention the answer i change the jquery :

<script language="javascript1.5" src="jquery-1.7.2.min.js"></script>
<script language="javascript1.5" src="generate_tabel.js"></script>

and

   $(".tbl_tr").on("click", function(){
      alert($(this).attr("kode"));
      //some function
    });

in Firefox error console no give message. but the alert doesnt run.

here my code: jsfiddle

solved:
i wrote simple code for simulation.

<script src="jquery-1.7.2.min.js"></script>
======================================================================<br />
<input type="button" id="btn" value="OK">
<div id="aaa"><a href=""><span >aaaaaaa</span></a>
</div>
<script>
$('#btn').click(function(){
        $('#aaa').html(' <table id="tbl" border=1><tr code="k1" class="ss"><td><a>aaa</a></td></tr><tr code="k2" class="ss"><td><a>bb</a></td></tr><tr code="k3" class="ss"><td><a>cc</a></td></tr></table>');
});
$('#aaa').on('click','#tbl tr a',function(a){
    alert($(this).closest('tr').attr('code'));
});
</script>

thank you all. I love you..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:20:20+00:00Added an answer on June 9, 2026 at 2:20 pm

    Since you’re dynamically creating the table. You need to delegate. Since it looks like you are creating the table inside of the #div_body element, it would probably be the most efficient parent element to use. The element must exist in the dom when working with the element

    By using delegation the .tbl_tr click event will bubble up to the #div_body and that is where the event will be handled. By doing that it also reduces the amount of event handlers attached since by binding you would have attached one for every .tbl_tr element, now you just have one handling the events

    If using jQuery 1.7+ use .on()

    $('#div_body').on('click','.tbl_tr',function(){
        alert($(this).attr("kode"));
    });
    

    or .delegate() if below 1.7

    $('#div_body').delegate('.tbl_tr','click',function(){
        alert($(this).attr("kode"));
    });
    

    Use above for dynamically created elements


    The two styles below are eventually the same thing

    $(".tbl_tr").on("click", function(){
          alert($(this).attr("kode"));
          //some function
    });
    $(".tbl_tr").click(function() {
          alert($(this).attr("kode"));
          //some function
    });
    

    Use only when you know the element will be there when dom is loaded or you are attaching after the element has been added to the dom

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I have a table that prints out vertical but I would like it
I have a big problem. I want to extract text from html table that
Here is the problem: I have two columns in a table that, for each
Screenshot mockup: http://tinypic.com/r/y2qex/5 Problem: I have a table that has 53 columns; one for
that my problem: I have database table like that: id (AI) market_id 1 6
I have a table that is split into 3. My problem is I have
I have a class that is mapped to a table using NHibernate. The problem
I have a table with more than 1 million records. The problem is that
I have a multi-table SQL query. My need is: The query should I generate
I have a problem in sql where I need to generate a packing list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.