Count divs inside div loaded from external php file

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1680 Views - Last Post: 11 February 2013 - 06:12 PM

#1 amatzprogrammer  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 07-July 11

Count divs inside div loaded from external php file

Posted 11 February 2013 - 11:09 AM

I have two php files, child.php and parent.php. Child.php generates div. Now, the parent.php calls child.php to be loaded in <div id='divContainer'>. Now, I need to count the div inside the divContainer. Is it possible? I tried something like this:

<script type='text/javascript'>
	  	     $(window).load(function(){
			 alert($('#divsContainer > div').length);
		     });	
	       </script>


However, it keeps giving me 0 value. I think what happens is that, the alert fires before the #divsContainer is fully loaded. How can I resolve this?

Is This A Good Question/Topic? 0
  • +

Replies To: Count divs inside div loaded from external php file

#2 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 11:14 AM

<script type="text/javascript">
	$(document).ready(function()
	{
		alert($("#divsContainer div").size());
	});
</script>


Assuming you have Jquery included before that code:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

Was This Post Helpful? 0
  • +
  • -

#3 amatzprogrammer  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 07-July 11

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 11:18 AM

Thanks! But I got same result as 0 in my alert.
Is there a way to make sure that everything is fully loaded already using jquery?
Was This Post Helpful? 0
  • +
  • -

#4 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 11:22 AM

document.ready should do just that.

btw I was assuming you are including the PHP files using:

<?php require "child.php"; ?>

This post has been edited by TheKirk: 11 February 2013 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 amatzprogrammer  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 07-July 11

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 11:25 AM

yes. but in this case, its not applicable. Since alert finishes first before the loading external file. And so, the value will always be 0.
Was This Post Helpful? 0
  • +
  • -

#6 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 11:26 AM

No:

http://api.jquery.com/ready/

Check google chrome's Developer Inspector > Console tab for errors.

This post has been edited by TheKirk: 11 February 2013 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#7 amatzprogrammer  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 07-July 11

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 11:28 AM

//

This post has been edited by amatzprogrammer: 11 February 2013 - 11:45 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,251
  • Joined: 08-June 10

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 11:40 AM

then you donít load the divs via PHP, because that would be done before the HTML even reaches the browser.
Was This Post Helpful? 0
  • +
  • -

#9 amatzprogrammer  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 07-July 11

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 11:44 AM

How can I load the php file and how can I count the divs inside a div?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,251
  • Joined: 08-June 10

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 11:53 AM

View Postamatzprogrammer, on 11 February 2013 - 07:44 PM, said:

How can I load the php file ?

// server-side:
include "file.php";

// client-side (jQuery)
$(parent_element).load("file.php");

// count (jQuery)
// parent_div being the ID of the parent div
// of course that needs to be after the AJAX call
// see http://api.jquery.com/load/ for further details
$("parent_div div").size();

Was This Post Helpful? 0
  • +
  • -

#11 amatzprogrammer  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 07-July 11

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 12:07 PM

I got the same result sir.
Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,251
  • Joined: 08-June 10

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 12:08 PM

so you didnít read the manual?
Was This Post Helpful? 0
  • +
  • -

#13 amatzprogrammer  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 07-July 11

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 12:10 PM

include 'Files.php';

<script type='text/javascript'>
      $('#divs').load("displayFiles.php");
      $(document).ready(function(){
       alert($("#divs div").size());
     });
</script>




--I got no error but wrong result.. Is that correct?

This post has been edited by amatzprogrammer: 11 February 2013 - 12:11 PM

Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,251
  • Joined: 08-June 10

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 12:12 PM

of course. AJAX doesn’t have anything to do with $().ready().

This post has been edited by Dormilich: 11 February 2013 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#15 amatzprogrammer  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 07-July 11

Re: Count divs inside div loaded from external php file

Posted 11 February 2013 - 12:15 PM

ok sir. sorry. I'll try again.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2