简体   繁体   中英

jquery onclick hide is not working at all, where am I going wrong

I have an unordered list whose list items have tables. These list items are draggable and sortable. I am working on hiding a list element whenit is clicked. I have a jquery code but it doesn't seem to work. please help me out of this problem. the html code is:

<ul class="sortable">
            <li class="list" id="first">1</li>
            <li class="list" id="second">2</li>
            <li class="list" id="third">3</li>
            <li class="list" id="fourth">4</li>
            <li class="list" id="fifth">5</li>
            <li class="list" id="sixth">6</li>
            <li class="list" id="seventh">7</li>
            <li class="list" id="eighth">8</li>
            <li class="list" id="ninth">9</li>
            <li class="list" id="tenth">10</li>

        </ul>

The jquery used is:

<script type="text/javascript">
        $(function(){
            $( ".sortable" ).sortable();
            $( ".sortable" ).disableSelection();
        });

        //ONCLICK CLOSE

        $(".list").click(function() {
            var tblId=$(this).attr("id");
            alert(tblId);
            $("#"+tblId).hide();
        });
</script>

There are some that i have used, I am not sure that are these links becoming a hindrance in the functionality. The links in the page are:

<!--script src="js/jQuery.min.js" type="text/javascript"></script-->
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<!--script src="js/jquery-ui.js" type="text/javascript"></script-->
<link rel="stylesheet" type="text/css" href="css/jquery.contextmenu.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/menu.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css" />
<script src="js/jquery.contextmenu.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />

You need to wrap the code in document.ready

 $(document).ready(function(){
 $(".list").click(function() {
        var tblId=$(this).attr("id");
        alert(tblId);
        $("#"+tblId).hide();
    });
});

You need to wrap your click function inside DOM ready handler.

$(function () {
    $(".sortable").sortable();
    $(".sortable").disableSelection();

    $(".list").click(function () {
        var tblId = $(this).attr("id");
        alert(tblId);
        $("#" + tblId).hide();
    });
});

Currently, only first block of your code are inside $(function() {...});

Also, you just need to include jQuery once, so you can remove:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

and move your jquery.contextmenu.js below jQuery UI script:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>  
<script src="js/jquery.contextmenu.js"></script>

As other answers suggested, move it inside dom ready handler

jQuery(function ($) {
    $(".sortable").sortable();
    $(".sortable").disableSelection();

    //move it inside the dom ready handler
    //also there is no need to fetch the id of the element, you can pass the dom element reference directly to jQuery
    $(".list").click(function () {
        $(this).hide();
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM