daniel.tosaba
daniel.tosaba

Reputation: 2563

jquery onclick by class name - how to

i have: <a class='category' href='view.php?category=1'>category</a>

i do: $("a.category").click(function(){alert("dsafdfad")})

and nothing happens??

start("view.php?"); posts("view.php?");

function start(link){
    $("#country ul").load(link+"mode=start",function(){
    $("#country a").click(function(){
        var link=encodeURI($(this).attr("href"));
        category(link+"&");
        posts(link+"&");
        return false;
        });
    });
}

function start2(link){
    $("#country ul").load(link+"mode=start",function(){
    $("#country a").click(function(){
        var link=encodeURI($(this).attr("href"));
        posts(link+"&");
        return false;
        });
    });
}

function posts(link){
    $("#posts").load(link+"mode=posts",function(){
    $("a.country").click(function(){
        var link=encodeURI($(this).attr("href"));
        category(link+"&");
        posts(link+"&");
        return false;
        });
    $("a.category").click(function(){
        $("#category").css("display","none");
        var link=encodeURI($(this).attr("href"));
        start2(link+"&");
        posts(link+"&");
        return false;
    });
    });
}

function category(link){
    $("#category ul").load(link+"mode=verNav",function(){
        $("#category").css("display","block");
        $("#category a").click(function(){
        var link=encodeURI($(this).attr("href"));
        posts(link+"&");
        return false;
        });
    });
}

Upvotes: 3

Views: 15972

Answers (3)

ysrb
ysrb

Reputation: 6740

Make sure that you don't have "alert" as local variable or overriding the jQuery "$".

Upvotes: 1

David
David

Reputation: 218798

Works for me. A few things to check:

Upvotes: 1

BalusC
BalusC

Reputation: 1108577

You need to ensure that the jQuery line is called when the element is already been created and present in the HTML DOM. In other words, do it during $(document).ready() in page head

<head>
    ...
    <script>
        $(document).ready(function() {
            // Here.
        });
    </script>
</head>  

or put the script at bottom of the page body, after the elements of interest.

<body>
    ...
    <script>
        // Here.
    </script>
</body>

Upvotes: 4

Related Questions