0

any one can help me i have gallery and every li has data-title, i want to add that value to url, im trying with no luck its always saying undefined, but i have post id and i easily added that to url. any one knows how to pass that data-title attribute to url.

    function getPhoto(postId, hasSelector) {

    //first visible value
    var title = jQuery(this).children("li").attr("data-title");
    var next = jQuery('#gallery-wrap li.visible').first().attr('id').split('photo-')[1];

    var next_title = jQuery('#gallery-wrap li.visible').first().attr('data-title')[1];

    //last visible value
    var prev = jQuery('#gallery-wrap li.visible').last().attr('id').split('photo-')[1];

    var prev_title = jQuery('#gallery-wrap li.visible').last().attr('data-title')[1];

    //has next var
    var hasNext = jQuery(hasSelector).parent().next();

    //has prev var
    var hasPrev = jQuery(hasSelector).parent().prev();


    //if there is a next object
    if(hasNext.length != 0) {

        while(hasNext.hasClass('visible') == false && hasNext.length != 0) {
            hasNext = hasNext.next();
        }

        if(hasNext.length != 0) {
            var next = hasNext.attr('id').split('photo-')[1];
            var next_title = hasNext.attr('data-title')[1];
        }
    }

    //if there is a previous object
    if(hasPrev.length != 0) {

        while(hasPrev.hasClass('visible') == false && hasPrev.length != 0) {
            hasPrev = hasPrev.prev();
        }

        if(hasPrev.length != 0) {
            var prev = hasPrev.attr('id').split('photo-')[1];
            var prev_title = hasPrev.attr('data-title')[1];
        }
    }


    // prev ids and post ids of photo   
    var _this = jQuery('#photo-'+postId);

    var prevIds = [];
    var nextIds = [];
    var nextTitle = [];
    var prevTitle = [];
    var newTitle = [];

    var nextLi = _this.next();
    while (nextLi.length != 0 && nextLi.hasClass('visible')) {
        nextIds.push(nextLi.attr('id').split('photo-')[1]);
        nextLi = nextLi.next();
        if (nextIds.length >= 5) { break; }
    }

    var prevLi = _this.prev();
    while (prevLi.length != 0 && prevLi.hasClass('visible')) {
        prevIds.unshift(prevLi.attr('id').split('photo-')[1]);
        prevLi = prevLi.prev();
        if (prevIds.length >= 5) { break; }
    }

    jQuery('#loader').fadeIn(200, function() {

        jQuery.ajax({
            url: jQuery('#theme-url').attr('data-id'),
            data: {id : postId, next : next, prev : prev, prev_ids: prevIds, next_ids: nextIds, nextTitle: next_title, prevTitle:prev_title, title:newTitle },
            success: function(data) {

                var gallery = jQuery('#gallery-overlay');

                gallery.html(data);

                jQuery('#next-post').attr('data-id', next);

                jQuery('body').css({overflow: 'hidden'});

                jQuery('#gallery-overlay, #body-overlay').fadeIn(500, function () {
                    jQuery('#loader').fadeOut(500);
                });
                //re-initialize the gallery
                galleryInit();
                var url = postId;
                var sTit = title
                if(url) {// require a URL
                    window.location.hash = url + sTit;
                }
            }
        });

    });
}

here is html code

<div id="gallery-wrap" class="clearfix">
  <ul class="large-grid">
    <li id="photo-417" class="term-9 visible">
      <a href="url" class="singleLightbox" data-title="data-title">
    <img" src="img" class="attachment-large-grid wp-post-image" alt="fotografia ślubna sesja nad polskim morzem" title="title" /></a></li>
    <li id="photo-333" class="term-4 visible">
       <a href="url" class="singleLightbox" data-title="data-title">
          <img src="img" class="attachment-large-grid wp-post-image" alt="sesja z trzymiesięcznym dzieckiem" title="title" /></a>
    </li>
  </ul>
 </div>
Digital Legend
  • 149
  • 1
  • 13
  • Can you show the `HTML` `.children()` gives you the immediate children of the selector. And you are trying to get the attribute on it.. Use a `$.each` instead and get the attribute of each li.. And you can also access the data attribute using this syntax.. `.data('title')` – Sushanth -- Jul 20 '13 at 20:31
  • I have been tried and its still undefined – Digital Legend Jul 21 '13 at 18:56
  • Can you post a small working example of what is not working , maybe in a fiddle – Sushanth -- Jul 21 '13 at 19:55

0 Answers0