3

i have got

<div><a class='link' href='index.php' data-container='target'>Load</a></div>
<div class='target'></div>

i need to write jquery code which load href url to data-container attribute

$('.link').click(function(e) {
    e.preventDefault();
    $(this.data-container).load(this.href);
});

3 Answers 3

2

Use data like this:

$('.link').click(function(e) {
    e.preventDefault();
    $('.' + $(this).data('container')).load(this.href);
});
Sign up to request clarification or add additional context in comments.

Comments

1
$('.link').click(function(e) {
    e.preventDefault();
    var container = $(this).data('container');
    $('.'+ container).load(this.href);
});

Comments

0

I reply to my own question to clear solution and make it better

<div><a class='link' href='index.php' data-target='.target-class'>Load</a></div>
<div class='target-class'></div>

<div><a class='link' href='index.php' data-target='#target-id'>Load</a></div>
<div id='target-id'></div>

jQuery script, I use fix from Claudio as a base for my modification (Blaster is the same but shorter)

$('.link').click(function(e) {
    e.preventDefault();
    var target = $(this).data('target');
    $(target).load(this.href);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.