0

Hi, so i found this topic on forum.jquery Click Here and there is a way to set and ID for a dynamically created div (copy paste from the forum):

var x=3,y=3
for (var i=0; i<x; i++){
    var line=$('<div>', {id:"X"+i } ).css({clear:'both',width:'100%'}).appendTo('body');
    for(var j=0;j<y;j++){
        $('<div>',{id:"X"+i+"_"+j}).css({float:'left',width:10,height:10,border:'2px solid red'}).appendTo(line)
    }
}

the question was asked two years ago so i dunno if i would have gotten a reply for my question that is why I a asking here. sorry though coz i know a lot of you dont like getting links that redirects outside of dani.

anyway my question is if the above code does create dv dynamically and gives them and id dynamically then what if we wanted to call that a particular how to do that?

the ids of the divs would be something like this i guess : X0, X1, X2 etc

for example 10 divs have been dynamically created then I'd like to maybe onClick the div so it would do something after the onClick like when clicking a button alert comes out "Clicked" we use either class, id or input type=button things like that t call the button but what about an element that we have no idea what the id would be. unless im just overthinking things.

2
Contributors
1
Reply
11
Views
2 Years
Discussion Span
Last Post by AleMonteiro
0
//You can do it normally...
$("#X3").click(function() {
    // Do Something
});

//Or you can append listener at the creation...
var x=3,y=3
for (var i=0; i<x; i++){
    var line=$('<div>', {id:"X"+i } ).css({clear:'both',width:'100%'}).appendTo('body');
    for(var j=0;j<y;j++){
        $('<div>',{id:"X"+i+"_"+j}).css({float:'left',width:10,height:10,border:'2px solid red'})
            .click(function() {
                // Do Something
            })
            .appendTo(line);
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.