0

Hi I have a dynamically generated list of items generated by php mysql.

Below a content section there is a link basically "Load Related" link in this case the content is a bunch of thumbnails. I dont want to load the thumbs with the list when the page loads, instead I want to load them with Ajax using JQuery.

The code below loads the data triggered by clicking an anchor "a.related" into one statically named div eg "div#pimgs" however it populates all divs named #pimgs

$('a.related').bind('click', function(e) {           
    var url = $(this).attr('href');
    $('div#pimgs').load(url);
    e.preventDefault();
});

<a class="related32" href="external-content.php?id=32"></a> 
<div id="pimgs32"></div>

I want to load data driven content into a dynimacilly named div eg"div#pimgs123" when you click an anchor named say "a.related123" or into a JQuery generated container directly below the anchor which is clicked.

Can someone help with this? Hope I'm making some sense...

4
  • 1
    sounds like you're using the same ID for different divs, if that's the case, remember that IDs must be unique in the page Commented Jul 24, 2014 at 16:58
  • Yes I'm using the MYSql row id like id="pimgs(my-record-id)" Commented Jul 24, 2014 at 17:00
  • include an example of the generated html Commented Jul 24, 2014 at 17:02
  • <a class="related32" href="external-content.php?id=32"></a> <div id="pimgs32"></div> Commented Jul 24, 2014 at 17:06

2 Answers 2

2

Can you start from this?

$( 'a.related' ).bind( 'click', function( e ) {  
    var url = $( this ).attr( 'href' );
    var id = $( this ).attr( 'data-id' );
    $( 'div#pimgs' + id ).load( url );
    e.preventDefault();
} );

In your dynamically generated <a>, put the numbers into data-id attribute (ref).

So maybe:

 <a href="foo" class="related" data-id="<?php echo $id; ?>">bar</a>
 <div id="pigms<?php echo $id; ?>"></div>
Sign up to request clarification or add additional context in comments.

1 Comment

This worked a dream.... never thought about using a data-id attribute.. Thanks rlatief
0

demo

Don't use duplicate ID's.

$('a[class^="related"]').bind('click', function(e) {
    e.preventDefault();
    var id = $(this).attr('class').split(' ').filter(function(v) {
        return v.indexOf('related') > -1;
    })[0];           
    var url = $(this).attr('href');
    $('#' + id).load(url);
});

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.