JavaScript jQuery - Identify td that was clicked jquery identify which button is clicked,jquery identify this

I have created a grid (table) inside of an HTML doc based on user preference.

After appending the rows and cells to the table, it appears jQuery cannot access them properly? Its as if it doesn't consider them part of the DOM.

Essentially I will be handling everything inside of a multidimensional array for my data, such as:

[ ["","",""], ["","",""], ["","",""] ]

It is currently set up with appended rows named #row_0, #row_1, etc and td's added without an ID.

I need to be able to map the clicked TD to my jQuery so I can perform some functions and logic on it.

Is there any way to respond to my JS when a user clicks on a td with something like:

tr = [1]

td = [2]

Current code found here:

http://jsfiddle.net/HNjMR/4/

Answer:1

Here live example friend: http://jsfiddle.net/HNjMR/5/

$("#board").on('click', 'td', function(){
    $("#board td").css("background","white");
    $(this).css("background","red");
});

This happens because when you insert a new dynamic element on the page, ready jquery can not find it. The ready by default runs events only on existing elements at load time. While some functions like .on, solve this problem.

Answer:2

http://jsfiddle.net/898n2/1/

$( document ).on( "click", "td", function() {
  alert(this.id);
});

This is using your current code

Notice I have used .on - this is a 'live' event using jquery

I have also modified your naming of each td to read col_1_2 - with 1 being col and 2 being row.

        $("#row_"+i).append("<td id='col_"+j+"_"+i+"'>&nbsp;</td>");
Answer:3

when the td element click handler is added those target elements are not yet created so use event delegation and bind the click handler to the #board element with the target element selector as td as given below. Then use .index() to find the position of row and td(Add 1 to index as it is 0 based)

$("#board").on('click', 'td', function () {
    var $td = $(this);
    var td = $td.index() + 1,
        row = $td.parent().index() + 1;
    alert(td + '-' + row);
});

Demo: Fiddle


Also your create code is buggy, jQuery append does not work like string concatenation

$('#board tr').remove();
var size = parseInt($("#size").val());
for (var i = 0; i < size; i++) {
    var $tr = $('<tr />', {
        id: 'row_' + i
    }).data('index', i + 1);
    $("#board").append("<tr id='row_" + i + "'>");
    for (var j = 0; j < size; j++) {
        $('<td />', {
            id: 'col_' + j
        }).data('index', j + 1).appendTo($tr);
    }
    $("#board").append($tr);
}

Demo: Fiddle

Answer:4

I am trying to trigger the button.click event of the page 1 from Popup page. There is Grid-View in Page 1 with the edit button for each row. when the button is clicked the popup page is opened. User ...

I am trying to trigger the button.click event of the page 1 from Popup page. There is Grid-View in Page 1 with the edit button for each row. when the button is clicked the popup page is opened. User ...

  1. trigger button click on enter
  2. trigger button click jquery
  3. trigger button click javascript
  4. trigger button click on enter react
  5. trigger button click angular
  6. trigger button click on enter key angular
  7. trigger button click react
  8. trigger button click on enter key jquery
  9. trigger button click c#
  10. trigger button click angular 6
  11. trigger button click on page load jquery
  12. trigger button click js
  13. trigger button click on page load
  14. trigger button click on enter key html
  15. trigger button click on enter angular
  16. trigger button click vuejs
  17. trigger button click event in javascript
  18. trigger button click using javascript
  19. trigger button click react js
  20. trigger button click from code behind

I am trying to push only distinct value in jQuery. But it is not working var arr = []; for (var i = 0; i < a.length; i++) { if (arr.indexOf({ A: a[i].City }) == -1) ...

I am trying to push only distinct value in jQuery. But it is not working var arr = []; for (var i = 0; i < a.length; i++) { if (arr.indexOf({ A: a[i].City }) == -1) ...

What does the following line do? (flag)?flag=false:flag=true; In this piece of code: var flag = true; function resize() { if(flag) { document.getElementById("centerimg")....

What does the following line do? (flag)?flag=false:flag=true; In this piece of code: var flag = true; function resize() { if(flag) { document.getElementById("centerimg")....

Is there in the world any compiler for a simple functional language such as that defined on this article, or maybe just Scheme or even ML for itself? That is, a compiler that will analyze the code for ...

Is there in the world any compiler for a simple functional language such as that defined on this article, or maybe just Scheme or even ML for itself? That is, a compiler that will analyze the code for ...