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

  • SEARCH
  • Home
  • 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 9039931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:51:18+00:00 2026-06-16T09:51:18+00:00

i have a problem in javascript [NOT JQUERY] ; i want to create slideshow,

  • 0

i have a problem in javascript [NOT JQUERY];
i want to create slideshow, for each slide i create that want when user click on it, jump to that slide…

var images = $('#images img'), image;

for(var i = 0; image = images[i]; i++)
{

    var a = document.createElement('span');
    a.onclick = function(){
        var img = image;
        removeClass();
        console.log(img);
        img.className = 'active';
    }

    $('#nav')[0].appendChild(a);

}

in above code i am trying remove active class name from slides and then add active to related slide, but i don’t know how can get that slide in span.onclick function

My Code:

<!doctype html>

<head>

    <title>Welcome</title>
    <style>
        #images {position:relative;}
        #images img {position:absolute;top:0;right:0;opacity:0;transition:all 0.8s;-webkit-transition:all 0.8s;}
        .active {opacity:1!important;}
        #nav span {display:inline-block;width:12px;height:12px;background:red;border:1px solid #CCC;cursor:pointer;}
    </style>
</head>
<body>
    <div id="images">
        <img src="1.jpg" class="active" />
        <img src="2.jpg" />
        <img src="3.jpg" />
        <img src="4.jpg" />
        <img src="5.jpg" />
        <img src="6.jpg" />
    </div>
    <div id="nav"></div>
</body>


<script>

    function $(id)
    {
        return document.querySelectorAll(id);
    }

    function removeClass(){
        var images = $('#images img'), image;
        for(var i = 0; image = images[i]; i++)
        {
            image.className = '';
        }
    }


    var images = $('#images img'), image;

    for(var i = 0; image = images[i]; i++)
    {

        var a = document.createElement('span');
        a.onclick = function(){
            var img = image;
            removeClass();
            console.log(img);
            img.className = 'active';
        }

        $('#nav')[0].appendChild(a);

    }

    function start(){
        return setInterval(function()
        {
            var current = $('.active')[0];
            var next = current.nextElementSibling;
            if(!next){
            console.log('noefds f as');
                next = $('#images img:first-child')[0];
            }
            current.className = '';
            next.className = 'active';

        },1000);
    };

    slide = start();

    var holder = $('#images')[0];

    holder.onmouseenter = function(){

        clearInterval(slide);

    }

    holder.onmouseleave = function(){
        slide = start();
    }

</script>

  • 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-06-16T09:51:19+00:00Added an answer on June 16, 2026 at 9:51 am

    JavaScript doesn’t have block scope, so all of the click handlers you create are referring to the same image variable. You can create a scope by introducing a new function and calling it immediately:

    var a = document.createElement('span');
    a.onclick = (function(img){
        return function() {
            removeClass();
            img.className = 'active';
        };
    })(image);
    

    Or slightly cleaner, but you may need to shim forEach in oldIE:

    var images = $('#images img'), nav = $("#nav")[0];
    [].forEach.call(images, function(image){
        var a = document.createElement('span');
        a.onclick = function(){
            removeClass();
            image.className = 'active';
        }
        nav.appendChild(a);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using javascript and jQuery. I have a problem that my web app has
I have one problem with Javascript Nodes.I want to find out what button was
I have a problem with JavaScript magic. When I execute this code: var page
I have a problem with Javascript that keeps bugging me for quite a while
I have a problem with Javascript Literal functions. for (curitem in array) { var
I have a problem with a javascript set of functions that I made. This
I have problem with dynamically created image (JavaScript). I want to change the innerHTML
Hi I have a problem with a javascript string var foo = \<a href=javascript(foo('a','b'))>test</a>\
The problem: I have a jQuery heavy page that has a built in admin
I have problem with javascript code. I am using google maps and I am

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.