Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello team,

I am facing a problem in my jquery. It is not workin on first time click. After that it is working fine.

This is my html page:
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
</head>

<script type="text/javascript">
    clickMenu = function (menu) {
        var getEls = document.getElementById(menu).getElementsByTagName("LI");
        var getAgn = getEls;

        for (var i = 0; i < getEls.length; i++) {
            getEls[i].onclick = function () {
                for (var x = 0; x < getAgn.length; x++) {
                    getAgn[x].className = getAgn[x].className.replace("click", "");
                }
                this.className += "click";
            }
        }
    }
</script>
<style>
    #gallery {
        width: 750px;
        height: 360px;
        position: relative;
        border: 1px solid #ddd;
        background: #f8f8f8;
    }

        #gallery ul {
            list-style: none;
            padding: 5px;
            margin: 0;
            width: 195px;
            float: right;
            height: 219px;
            overflow: auto;
        }

            #gallery ul li {
                display: inline;
                width: 50px;
                height: 50px;
                float: left;
                margin: 0 4px 4px 0;
                border: 1px solid #444;
                cursor: pointer;
            }

                #gallery ul li img {
                    display: block;
                    width: 50px;
                    height: 50px;
                }

                #gallery ul li span {
                    display: none;
                    position: absolute;
                    left: 547px;
                    top: 235px;
                    width: 200px;
                    font-family: verdana, arial, sans-serif;
                    color: #06a;
                    font-size: 11px;
                    line-height: 15px;
                    text-align: justify;
                }

                    #gallery ul li span b {
                        color: #444;
                    }

                /* required for IE5.x and IE6 to display the start image size correctly */
                #gallery ul li.click i img.default {
                    margin: 5px auto 0 auto;
                    width: 320px;
                    height: 240px;
                    border: 1px solid #fff;
                }

                #gallery ul li.off {
                    display: none;
                }

                #gallery ul li.click {
                    border-color: #fc0;
                    background: #f8f8f8;
                    cursor: default;
                }

                    #gallery ul li.click i {
                        position: absolute;
                        left: 0;
                        top: 0;
                        width: 545px;
                        height: 355px;
                    }

                        #gallery ul li.click i img {
                            margin: 5px auto 0 auto;
                            width: auto;
                            height: auto;
                            border: 1px solid #fff;
                        }

                    #gallery ul li.click span {
                        display: block;
                    }
</style>

<body>
    <div onclick="clickMenu('gallery')">
        <div id="gallery">
            <ul>
                <li><i>
                    <img src="images/nature1.jpg" title="" alt="" /></i></li>
                <li><i>
                    <img src="images/nature2.jpg" title="" alt="" /></i></li>
                <li><i>
                    <img src="images/nature3.jpg" title="" alt="" /></i></li>
                <li><i>
                    <img src="images/nature4.jpg" title="" alt="" /></i></li>
                <li><i>
                    <img src="images/nature5.jpg" title="" alt="" /></i></li>

                <li class="click"><i>
                    <img class="default" src="images/nature4.jpg" title="" alt="" /></i></li>
            </ul>
        </div>
    </div>


</body>
</html>
Posted
Updated 20-Dec-15 19:48pm
v2

1 solution

Add this code to your page inside script block :

With jquery :

JavaScript
$(document).ready(function () {
           clickMenu('gallery');
       });


And I don't see your jquery file reference. Please add reference of your jquery file.
Eg :
from local path :
JavaScript
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>


Or from CDN :
JavaScript
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>


If you don't wanna use jqeury then add these codes inside script after clickMenu event :

JavaScript
window.onload = setTimeout(function () { clickMenu('gallery') }, 0);


Good luck.
 
Share this answer
 
v3
Comments
Deepak Tiwari (D'pak) 21-Dec-15 8:23am    
Thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900