views:

80

answers:

5

Good day,

I want to set click event on every anchor element in my div container. Here is an example what I want to do:

---HTML---
<div id="my-container">
    <a href="page1.html">page1</a>
    <a href="page2.html">page2</a>
    <a href="page3.html">page3</a>
</div>

---- jQuery ----
$("#my-container a").click(function() {
    var link = $(this).attr("href");
    $("#my-container").load(link);
});

What I want to do is to let me handle loading event of href clicks and load it to the same container. And this is must done without id, class attributes which aren't available for that hrefs. The problem is in this: $("#my-container a"). Any help would be appreciated! Thanks

UPDATE

People doesn't seem to get right what I wanted to ask. I repeat myself again. $("#my-container a") <---- doesn't add click events on href anchors. So how I can set click event?

+1  A: 

You forgot to quote the href string literal:

var link = $(this).attr("href");
                        ^    ^

Also, you will need to cancel the default behavior of the click event. Currently, your event handler would fire, but you would not see the result, as the browser continues to follow the link you have clicked. Add a return false; as the last line of the event handling function to cancel this behavior.

Jørn Schou-Rode
Fixed that, but its because I written this simple example fast for this question. But main problem is not in this :)
faya
@faya: See my update (or any of the other answers which seems to point out the lack of `return false` as well).
Jørn Schou-Rode
The problem is in setting the event handler on anchors inside container :)
faya
@faya: With the two corrections in place, it works fine on my machine. The problem lies somewhere else in your code. Start picking it apart, line by line, to find the culprit.
Jørn Schou-Rode
A: 

You can do like:

$(function(){
    $("#my-container > a").click(function() {
        var link = $(this).attr('href');
        $('#my-container').load(link);
        return false;
    });
});

But if you meant that you don't want to give the div any id or class then any div having links inside it will also be affected. So you should at least give the id to the div you want to load the content in.

Sarfraz
I meant no id or class attributes for the anchors. It is because my pagination framework doesn't let me add automatically id or class attributes to generated anchors. And as you can see div has id attritbute.
faya
@faya: The code seems to be fine, what error do you get?
Sarfraz
I don't get any error. I set for the test $("#my-container > a").click(function() { alert('test')});. I click link and alert doesn't get invoked. As far as I know it means click event isn't set. I also tried with $.live, but seems its the same...
faya
@faya: Make sure that you have include the jquery file and path is correct.
Sarfraz
A: 

Add return false;.

$("#my-container a").click(function() {
    var link = $(this).attr("href");
    $("#my-container").load(link);
    return false;
});
Sam
+3  A: 

Try this, wasn't sure if you were missing any tags so I've put the whole thing in:

<script type="text/javascript">
     $(document).ready(function(){
        $("#my-container a").click(function(event) {
            alert('test');
            var link = $(this).attr("href");
            $("#my-container").load(link);

            event.preventDefault();
        });
     });

</script>
A: 

Have you try it with the bind function:

$("#my-container a").bind("click", function() {
    var link = $(this).attr("href");
    $("#my-container").load(link); 
});
mathk