1

How can I access a dynamically created element using jQuery? Suppose I have the following code:

for (var i = 0; i < count; i++)
{
    var div = document.createElement("div");
    div.setAttribute("id", "divHour" + i);

    var button = document.createElement("button");
    button.setAttribute("id", "btnHour" + i);

    div.appendChild(button);
    document.getElementById("divHours").appendChild(div);
}

How can I access the buttons using jQuery?

1
  • bind them some data that you can use later. id, class, rel, anything should work. You can also use jquery's data function. Commented Nov 24, 2012 at 20:04

5 Answers 5

1

To select the button inside your original loop...

for (var i = 0; i < count; i++)
{
    var div = document.createElement("div");
    div.setAttribute("id", "divHour" + i);

    var button = document.createElement("button");
    button.setAttribute("id", "btnHour" + i);

    div.appendChild(button);
    document.getElementById("divHours").appendChild(div);

    // moved after the button has been added to the DOM
    // do something with the button in jQuery
    $("#btnHour" + i).css({width:100})

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

3 Comments

$("#btnHour" + i).click( function () { alert('the button was clicked!'); } ); does not seem to be working...
It works now, first I had to append the buttons to the div, and the div to the document. Thanks again.
@burakk I put it last in your code block, which I think is how you got it working right?
1

As long as you know the HTML ID of the element. All you need to do is this:

$("#html_id")

jQuery uses CSS selectors.

Comments

1

Give the buttons a class:

div.setAttribute("class", "myButton");

Then you can get all of the buttons with

$('.myButton') ...

For example, to loop over them:

$('.myButton').each(function(){

    console.log($(this).attr("id"));

});

If you want to identify each button, parse the number out of the class or give it a data-mynumber attribute and use $(this).data('mynumber')

Comments

1
var $button0 = $('#btnHour0')
var $button1 = $('#btnHour1')
// ... etc ...

Once you have cached the jQuery object, use it as you wish...

$button0.css({width: 400}).animate({width: 200})

EDIT To access all buttons in a loop...

// assuming `count` is the same as the code used to create the buttons
for (var i = 0; i < count; i++){
    var $button = $('#btnHour'+i)
    // do stuff with $button here
}

EDIT

Alternatively, to access all button elements that have an ID that starts with btnHour

var $buttons = $('button[id^="btnHour"]')
// do stuff to all buttons here
$buttons.css({width:300})

2 Comments

The problem is, I don't know how many buttons will be created, so I need a way like btnHour + i
the number of buttons will be the same as count
1
var buttons=$('button[id^="btnHour"]');

Will give you the whole collection of buttons

Your question is extremely vague, I suspect you want to access a specific button contained within a div that user interacts with. More details are required as to what you want.

EDIT: following is how you can access the index of a button within a click handler.

var buttons=$('button[id^="btnHour"]').click(function(){
   var buttonIndex= buttons.index(this);
    var div=$('#divHour'+ buttonIndex)
    /* can now interact with corresponding div*/
});

Another simpler way to find the parent div is :

$('button[id^="btnHour"]').click(function(){
   var $parentDiv=$(this).parent()
})

To target a specific button use eq() method

var thirdButton=$('button[id^="btnHour"]').eq(2);/* indexing is zero based*/

2 Comments

I'd like to access the buttons using jQuery just after they are created in the loop. For instance, $("#btnHour4"), how can I craft the jQuery selector using i, or is it not possible?
see update answer on how you can get the button index and matching div. You really don't need incremetal ID's if you use index() , can use common class and find matching div with jQuery eq(index) method also

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.