HighDots Forums  

How to determine number of elements after dom manipulation ?

jQuery jQuery is a fast, concise, JavaScript Library that simplifies how you traverse HTML documents, handle events, perform animations, and add Ajax interactions to your web pages. jQuery is designed to change the way that you write JavaScript.


Discuss How to determine number of elements after dom manipulation ? in the jQuery forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Richard Bakker
 
Posts: n/a

Default How to determine number of elements after dom manipulation ? - 11-07-2009 , 03:18 AM






I am generating elements (img's) based on data from an xml file.
Once that is done, I want to determine the number of icons that were
generated.

i do a: alert($('img').size())
result: 0, which isn't the case

how can i determine them after they have generated ?
**********************************************
function dataloader(location,service,div){
$.ajax({
type: "GET",
url: "includes/data.xml",
dataType: "xml",
success: function(xml) {

$(xml).find('group').each(function(){
if($(this).attr("name") == service){
$(this).find("Service").each(function(){
var type = $(this).find("Type").text()
var host = $(this).find("Host").text()
var name = $(this).find("Name").text()
var site = $(this).find("Site").text()

if(type == "Host" && site == location){
$('<img src="images/
server.jpg" title="'+ host +'" />').appendTo
('#'+div);
}
})
}
});
}
});

}

Reply With Quote
  #2  
Old   
Richard Bakker
 
Posts: n/a

Default Re: [jQuery] How to determine number of elements after dommanipulation ? - 11-08-2009 , 12:45 PM






Does anyone perhaps know ?


On 7 nov 2009, at 09:18, Richard Bakker wrote:

Quote:
I am generating elements (img's) based on data from an xml file.
Once that is done, I want to determine the number of icons that were
generated.

i do a: alert($('img').size())
result: 0, which isn't the case

how can i determine them after they have generated ?
**********************************************
function dataloader(location,service,div){
$.ajax({
type: "GET",
url: "includes/data.xml",
dataType: "xml",
success: function(xml) {

$(xml).find('group').each(function(){
if($(this).attr("name") == service){
$(this).find("Service").each(function(){
var type = $(this).find("Type").text()
var host = $(this).find("Host").text()
var name = $(this).find("Name").text()
var site = $(this).find("Site").text()

if(type == "Host" && site == location){
$('<img src="images/
server.jpg" title="'+ host +'" />').appendTo
('#'+div);
}
})
}
});
}
});

}

Reply With Quote
  #3  
Old   
Qamal Kosim-Satyaputra
 
Posts: n/a

Default Re: [jQuery] How to determine number of elements after dommanipulation ? - 11-08-2009 , 01:13 PM



where did you exactly call 'alert($('img').size())'? inside the callback
function? remember ajax is asynchronous, if you're calling it after calling
dataloader() the ajax mat not return yet and your images aren't yet in the
DOM.

On Mon, Nov 9, 2009 at 4:45 AM, Richard Bakker <richardbakker (AT) me (DOT) com> wrote:

Quote:
Does anyone perhaps know ?


On 7 nov 2009, at 09:18, Richard Bakker wrote:

I am generating elements (img's) based on data from an xml file.
Once that is done, I want to determine the number of icons that were
generated.

i do a: alert($('img').size())
result: 0, which isn't the case

how can i determine them after they have generated ?
**********************************************
function dataloader(location,service,div){
$.ajax({
type: "GET",
url: "includes/data.xml",
dataType: "xml",
success: function(xml) {

$(xml).find('group').each(function(){
if($(this).attr("name") == service){
$(this).find("Service").each(function(){
var type = $(this).find("Type").text()
var host = $(this).find("Host").text()
var name = $(this).find("Name").text()
var site = $(this).find("Site").text()

if(type == "Host" && site == location){
$('<img src="images/server.jpg"
title="'+ host +'" />').appendTo
('#'+div);
}
})
}
});
}
});

}



Reply With Quote
  #4  
Old   
Karl Swedberg
 
Posts: n/a

Default Re: [jQuery] How to determine number of elements after dommanipulation ? - 11-08-2009 , 01:18 PM



You'll have to do it within the success callback:

function dataloader(location,service,div){
$.ajax({
type: "GET",
url: "includes/data.xml",
dataType: "xml",
success: function(xml) {

$(xml).find('group').each(function(){
if($(this).attr("name") == service) {
$(this).find("Service").each(function(){
var type = $(this).find("Type").text()
var host = $(this).find("Host").text()
var name = $(this).find("Name").text()
var site = $(this).find("Site").text()

if(type == "Host" && site == location){
$('<img src="images/server.jpg" title="'+ host +'" /
Quote:
').appendTo('#'+div);
}
});
}
});
alert($('#' + div).find('img').length); // <-- should give you
correct number here
}
});
}

--Karl

On Nov 8, 2009, at 12:45 PM, Richard Bakker wrote:

Quote:
Does anyone perhaps know ?


On 7 nov 2009, at 09:18, Richard Bakker wrote:

I am generating elements (img's) based on data from an xml file.
Once that is done, I want to determine the number of icons that were
generated.

i do a: alert($('img').size())
result: 0, which isn't the case

how can i determine them after they have generated ?
**********************************************
function dataloader(location,service,div){
$.ajax({
type: "GET",
url: "includes/data.xml",
dataType: "xml",
success: function(xml) {

$(xml).find('group').each(function(){
if($(this).attr("name") == service){
$(this).find("Service").each(function(){
var type = $(this).find("Type").text()
var host = $(this).find("Host").text()
var name = $(this).find("Name").text()
var site = $(this).find("Site").text()

if(type == "Host" && site == location){
$('<img src="images/
server.jpg" title="'+ host +'" />').appendTo
('#'+div);
}
})
}
});
}
});

}

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.