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 1095693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:09:55+00:00 2026-05-17T00:09:55+00:00

How to rewrite the code using jQuery? <script type=text/javascript> window.onload = function(){ var array

  • 0

How to rewrite the code using jQuery?

 <script type="text/javascript">
    window.onload = function(){
        var array = document.getElementsByTagName('div');
        for (var i=0; i<array.length; i++) {
            (function(i) {
                array[i].onclick = function() {
                    alert(i);
                }
            })(i);
        }
    };
    </script>
    <div>0</div>
    <div>1</div>

Thanks…

  • 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-17T00:09:56+00:00Added an answer on May 17, 2026 at 12:09 am

    If you actually want to alert the index:

    Example: http://jsfiddle.net/Ksyr6/

       // Wrapping your code in an anonymous function that is passed as a parameter
       //    to jQuery will ensure that it will not run until the DOM is ready.
       // This is a shortcut for jQuery's .ready() method
    $(function() {
        $('div').each(function( i ) {
             $(this).click(function() {
                alert( i );
             });
         });
    });
    

    This finds all elements with the tag name 'div', and iterates over them, individually assigning a click event that alerts its index.

    Or if the index is not important, it becomes simpler:

    Example: http://jsfiddle.net/Ksyr6/1/

    $(function() {
        $('div').click(function() {
             // You have access to the element that received the event via "this"
             // alert(this.id) will alert the ID of the element that was clicked
            alert( 'i was clicked' );
         });
    });
    

    Here the same iteration is taking place, but it is implicit. jQuery iterates over the 'div' elements, giving each on the click event handler that fires an alert.

    This is a nice feature of jQuery. You pass it a CSS selector, it finds matching elements, and iterates over them automatically, firing whatever method you call.

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

Sidebar

Related Questions

I have to rewrite code that references a 2-dimensional array using pointers instead of
I'm using this code to hide a client's page until it's loaded: <style type=text/css>
How would you rewrite this statement using Jquery + Ajax only? This code displays
Using the JQuery autocomplete I have a function that produces an Array of friends
How can I rewrite this code without using jQuery? I need to do it
How can I rewrite he following code using C#3.0 automatic properties? private int _myValue;
How can I rewrite this code to coffeescript: $('.delete_action').click(function(event) { $.get('/delete_action?name=' + event.target.name +
I will be using some jQuery below this chunk of code (which I may
I am currently using this type of mod_rewrite code for all my page...but i
I am performing a simple AJAX() request using Jquery (Google hosted 1.7.1 jquery.min.js code)

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.