Luca Romagnoli
Luca Romagnoli

Reputation: 12475

find elements with jquery

i have a div:

<div id="div"></div>

with

$("#div").append("<div id='c'></div>")
$("#c").length 

returns 0. what i can do for find the div width id = c after inserted in div #div?

thanks

Upvotes: 2

Views: 127

Answers (5)

fish2000
fish2000

Reputation: 4435

First off, he div won't have a width until it contains some content. Try that.

Upvotes: -1

Nick Craver
Nick Craver

Reputation: 630587

What you have will work as long as you're running it in a document.ready handler, like this:

$(function() {
  $("#div").append("<div id='c'></div>")
  alert($("#c").length );  //alerts 1
});

You can test it here, without the .ready wrapper, if it runs before <div id="div"></div> is loaded in the DOM, that #div selector won't find anything, and so it won't append anything to the 0 elements it found.

Upvotes: 1

Nikita Rybak
Nikita Rybak

Reputation: 68026

It does work for me: http://jsfiddle.net/4Q4cM/

Maybe #div element isn't appended to DOM tree itself? E.g., you're doing $('<div id="div"></div>') instead of inserting it in the document. Nick Craver also has a good guess.

Upvotes: 2

shoebox639
shoebox639

Reputation: 2312

try doing:

$('div').append($('<div>').attr('id', 'c'))

Upvotes: 1

codersarepeople
codersarepeople

Reputation: 2001

$("#div #c")

will find what you're looking for

Upvotes: -2

Related Questions