Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6344117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:34:30+00:00 2026-05-24T20:34:30+00:00

sorry for my bad english. I’ve got such a question: why the first code:

  • 0

sorry for my bad english.

I’ve got such a question: why the first code:

<html>
  <head>
    <style>
      body { position: relative; }
      table { position: absolute; top: 200px; left: 200px; }
      div { width: 100px; height: 100px; background-color: black; margin: auto auto; }
    </style>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
    $(document).ready(function(){
      for(i = 0; i < 100; i++) {
        var j = i + '';
        $("#menu_div_" + j).mouseover(function(){
          $("#menu_div_" + j).clearQueue();
          $("#menu_div_" + j).stop();
          $("#menu_div_" + j).animate({
            width: 150,
            height: 150
          }, 600
          );
        });
        $("#menu_div_" + j).mouseout(function(){
          $("#menu_div_" + j).clearQueue();
          $("#menu_div_" + j).stop();
          $("#menu_div_" + j).animate({
            width: 100,
            height: 100
          }, 600
          );
        });
      }
    });
    </script>
  </head>
  <body>
    <table id="menu">
<? for($i = 0; $i < 10; $i++) { ?>
      <tr>
<?   for($j = 0; $j < 10; $j++) { ?>
        <td>
          <div id="menu_div_<? echo ($i * 10) + $j; ?>">
          </div>
        </td>
<?   } ?>
      </tr>
<? } ?>
    </table>
  </body>
</html>

doesn’t work exactly the same like in this code (“for” loop is written in php):

<html>
  <head>
    <style>
      body { position: relative; }
      table { position: absolute; top: 200px; left: 200px; }
      div { width: 100px; height: 100px; background-color: black; margin: auto auto; }
    </style>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script>
      $(document).ready(function(){
<?php for($i = 0; $i < 100; $i++) { ?>
        $("#menu_div_" + <?php echo $i; ?>).mouseover(function(){
          $("#menu_div_" + <?php echo $i; ?>).clearQueue();
          $("#menu_div_" + <?php echo $i; ?>).stop();
          $("#menu_div_" + <?php echo $i; ?>).animate({
            width: 150,
            height: 150
          }, 600
          );
        });
        $("#menu_div_" + <?php echo $i; ?>).mouseout(function(){
          $("#menu_div_" + <?php echo $i; ?>).clearQueue();
          $("#menu_div_" + <?php echo $i; ?>).stop();
          $("#menu_div_" + <?php echo $i; ?>).animate({
            width: 100,
            height: 100
          }, 600
          );
        });
<?php } ?>
      });
    </script>
  </head>
  <body>
    <table id="menu">
    <? for($i = 0; $i < 10; $i++) { ?>
      <tr>
    <?   for($j = 0; $j < 10; $j++) { ?>
        <td>
          <div id="menu_div_<? echo ($i * 10) + $j; ?>">
          </div>
        </td>
    <?   } ?>
      </tr>
    <? } ?>
    </table>
  </body>
</html>

Quick explanation:

In the first one, “events” from all “div” tags result in changing the size of the last element (I don’t know why). In the second – “event” from each “div” results in changing size of the same “div” (what, in my opinion, should also happen in the first code too).

How can I correct first code to make it work like the second one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:34:31+00:00Added an answer on May 24, 2026 at 8:34 pm

    j doesn’t equal what you think it will. It will ALWAYS come back with 99. This is due to the defining anonymous functions within a loop.

    Luckily, with a little jQuery magic, this is easily fixed. Rather than trying to reference each element directly, $('#menu_div_'+j), just use $(this). Inside jQuery event handlers, this is automatically set the to element/target of the event.

    $(document).ready(function(){
      for(i = 0; i < 100; i++) {
        var j = i + '';
        $("#menu_div_" + j).mouseover(function(){
          $(this).clearQueue();
          $(this).stop();
          $(this).animate({
            width: 150,
            height: 150
          }, 600
          );
        });
        $("#menu_div_" + j).mouseout(function(){
          $(this).clearQueue();
          $(this).stop();
          $(this).animate({
            width: 100,
            height: 100
          }, 600
          );
        });
      }
    });
    

    JSFiddle Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, sorry for my bad english. I have an EF entity that looks like:
sorry for bad English. It's Ruby code. s = мистика `touch #{s}` `cat #{s}`
Sorry for bad English :( Suppose i can preliminary organize recipes and ingredients data
Sorry for bad english. bfproduct is a table and productid is primary key in
sorry for bad english. i want to read element with a id but no
I`m from Russia, so sorry for bad English. I want to load template in
Sorry for my bad English. I just started working on my school final's project
Sorry for my bad English.. I read that the use of Codeigniter 2 with
Sorry for the bad title, but I couldn't think of a better one. I'm
Sorry for the basic question - I'm a .NET developer and don't have much

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.