2 Replies - 1578 Views - Last Post: 24 March 2012 - 02:33 AM

#1 ernestlwl   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 13-September 11

How can jQuery work for each looped objects by PHP?

Posted 24 March 2012 - 01:47 AM

Hello web designers, I have an issue regarding jQuery. I am not sure how 1 jQuery function "hover" can work for each div individually. Previously I've succeed by inputting variables using a regular Javascript method with a hrefs.
For example...
<?php
$i = 0
while( $i < 5 )
{
	echo "<div id='SomeNumber_". $i ."'>". $i ."</div>";
	echo "<a href='javascript:jFunc(". $i .");'> Click Me To Input Number ". $i ." </a>";
}
?>

<script>
function jFunc( i )
{
	$('#TextArea').val( $( '#SomeNumber_' + i ).text() );
}
</script>




But is this also possible to do this for the hover command for Divs in jQuery?
I want to make the div I'm hovering over, glows yellow.


<?php
$i = 0
while( $i < 5 )
{
	echo "<div id='Name_". $i ."'> This is div ". $i ."</div>";
}
?>

<script>
$(document).ready
(
	function()
	{
  		$("#Name_" + $i).hover		// DOES NOT WORK
		(
			function()
			{
    				$("#Name_" + $i).css("background-color","yellow");
    			},
			function()
			{
    				$("#Name_" + $i).css("background-color","#FFF");
 			}
		);
	}
);
</script>



Rather new to jQuery, if you know a method more simple and elegant than mine please do explain.
Thank you.

EDIT:
The only working solution I know is to make a jQuery function for each div individually. (ex. $("#Name_1"), $("#Name_2"), $("#Name_3") but this doesn't seem to be a practical method for huge amount of divs.

This post has been edited by ernestlwl: 24 March 2012 - 01:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How can jQuery work for each looped objects by PHP?

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4244
  • View blog
  • Posts: 13,449
  • Joined: 08-June 10

Re: How can jQuery work for each looped objects by PHP?

Posted 24 March 2012 - 02:12 AM

give all the <div>s a common class name and use that to access them in jQuery.

$(".someClass").hover(
  function() {
    $(this).css();
  },
  function() {
    $(this).css();
  }
);

Was This Post Helpful? 0
  • +
  • -

#3 ernestlwl   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 13-September 11

Re: How can jQuery work for each looped objects by PHP?

Posted 24 March 2012 - 02:33 AM

View PostDormilich, on 24 March 2012 - 02:12 AM, said:

give all the <div>s a common class name and use that to access them in jQuery.

$(".someClass").hover(
  function() {
    $(this).css();
  },
  function() {
    $(this).css();
  }
);


Works beautifully. Thank you very much !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1