0

addEventListener() not working ?

var mytable = document.getElementById('GridTable');

for (var r = 0; r <rows; r++) 
{
    var row = mytable.insertRow(r);
    for (var c = 0; c <cols; c++)
    {

        var cell=row.insertCell(c);
        cell.id='td'+nn;
        //alert(cell.id);
        cell.innerHTML='&nbsp';
        cell.className="dynamic";
        cell.addEventListener("click", function() {bcolor1('red',cell.id);});
        nn++;
    }

}
4
  • bcolor1 ?..... where is it? Commented Jan 23, 2015 at 9:34
  • 2
    Please post all the relevant code(with your HTML). The function bcolor1('red',cell.id) is missing. Commented Jan 23, 2015 at 9:34
  • 3
    you've tagged jquery but have none in your example, are you using jquery? Commented Jan 23, 2015 at 9:35
  • bcolor1('red',cell.id); is my function which apply background color to clicked cell. When I try to execute this code and click on any cell it is applying red color to only last cell of table, am totally confused plz help me ...... Commented Jan 23, 2015 at 10:34

3 Answers 3

1

You need to use event delegation to achieve this.

With Javascript, you can do it this way:

document.querySelector('element').addEventListener('click', function(event) {      
    // on click action here..
});

With jQuery, you can do it this way:

$(document).on("click", "element", function(){
  // on click action here..
});

Learn more: Understanding Event Delegation | jQuery

Sign up to request clarification or add additional context in comments.

Comments

0

You can use on() function to achieve event delegation.

This way, you can select every dynamicaly added element with class .dynamic

$(document).on('click', '.dynamic', function() {
    var that = $(this);
    that.css({'background-color': 'red'});
    // do your stuff here ..
});

1 Comment

informative purpose: double semicolon, syntax, line 2: var that = $(this;); should be var that = $(this);
0

I got the following solution and it is working fine .

 var mytable = document.getElementById('GridTable');
 for (var r = 0; r <trow; r++) 
    {
        temp=noc;
        var row = mytable.insertRow(r);
        for (var c = 0; c <tcol; c++)
        {
            cell=row.insertCell(c);
            cell.id='td'+temp;
            cell.style.width =cwidth;
            cell.style.height =cheight;
            //alert(cell.id);
            //cell.innerHTML='&nbsp;';
            cell.className="dynamic";

            with ({ n: cell.id }) 
            {
                cell.onclick = function()
                {
                        bcolor1('red',n);
                };
                cell.onmousemove=function()
                {
                    bcolor('red',n);
                }   
            }                                                         
            temp++;
        }
        noc=noc-tcol;
    } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.