2

UPDATE

I am trying to include the same set of buttons inside multiple div's.. It seems I can only include the button set on one div. It will only show up in one location at once. I want the button to show wherever I put that span set at. Help! Here's my code:

    (function($){

// Creating the jQuery plugin:
$.fn.sweetPages = function(opts){

    // If no options were passed, create an empty opts object
    if(!opts) opts = {};

    var resultsPerPage = opts.perPage || 3;

    // The plugin works best for unordered lists, althugh ols would do just as well:
    var ul = this;
    var li = ul.find('li');

    li.each(function(){
        // Calculating the height of each li element, and storing it with the data method:
        var el = $(this);
        el.data('height',el.outerHeight(true));
    });

    // Calculating the total number of pages:
    var pagesNumber = Math.ceil(li.length/resultsPerPage);

    // If the pages are less than two, do nothing:
    if(pagesNumber<2) return this;

    // Creating the controls div:
    var swControls = $('<div class="swControls">');

    for(var i=0;i<pagesNumber;i++)
    {
        // Slice a portion of the lis, and wrap it in a swPage div:
        li.slice(i*resultsPerPage,(i+1)*resultsPerPage).wrapAll('<div class="swPage" />');

        // Adding a link to the swControls div:
        swControls.append('<a href="" class="swShowPage">'+(i+1)+'</a>');
    }

    ul.append(swControls);

    var maxHeight = 0;
    var totalWidth = 0;

    var swPage = ul.find('.swPage');
    swPage.each(function(){

        // Looping through all the newly created pages:

        var elem = $(this);

        var tmpHeight = 0;
        elem.find('li').each(function(){tmpHeight+=$(this).data('height');});

        if(tmpHeight>maxHeight)
            maxHeight = tmpHeight;

        totalWidth+=elem.outerWidth();

        elem.css('float','left').width(ul.width());
    });

    swPage.wrapAll('<div class="swSlider" />');

    // Setting the height of the ul to the height of the tallest page:
    ul.height(maxHeight);

    var swSlider = ul.find('.swSlider');
    swSlider.append('<div class="clear" />').width(totalWidth);

    var hyperLinks = ul.find('a.swShowPage');

    hyperLinks.click(function(e){

        // If one of the control links is clicked, slide the swSlider div 
        // (which contains all the pages) and mark it as active:

        $(this).addClass('active').siblings().removeClass('active');

        swSlider.stop().animate({'margin-left':-(parseInt($(this).text())-1)*ul.width()},'slow');
        e.preventDefault();
    });

    // Mark the first link as active the first time this code runs:
    hyperLinks.eq(0).addClass('active');

    // Center the control div:
    swControls.css({
        'left':'50%',
        'margin-left':-swControls.width()/2
    });

    return this;

}})(jQuery);


$(document).ready(function(){
    /* The following code is executed once the DOM is loaded */

    // Calling the jQuery plugin and splitting the
    // #holder UL into pages of 3 LIs each:

    $('#holder').sweetPages({perPage:1});

    // The default behaviour of the plugin is to insert the 
    // page links in the ul, but we need them in the main container:

    var controls = $('.swControls').detach();
    controls.appendTo('#main');

    // Make Nav Buttons

    function swGotoPage(page){
    $('.swShowPage:contains("' + page + '")').click();
}

var baseFB = '<input type="button" class="swFB" />';
var offset = 'pgOffset';
var active = '.swShowPage.active';

var $pgBack = $(baseFB)
    .attr('id', 'button_back')
    .attr('value', "Back")
    .attr(offset, '-1');

var $pgForward = $(baseFB)
    .attr('id', 'button_forward')
    .attr('value', "Next")
    .attr(offset, '1');

$.each([$pgBack, $pgForward], function(i,$obj){
    $obj.click(function(){
        var nextPage =  parseInt($(active).text(), 10) + parseInt($(this).attr(offset), 10);
        swGotoPage(nextPage);
    });
});


$($pgForward).addClass("teach_create_backforward");
$($pgBack).addClass("teach_create_backforward");

$('.teach_create_pageheader_back').prepend($pgBack);
$('.teach_create_pageheader_forward').prepend($pgForward);


});

The HTML:

<li>
    <div class="noselect" id="teach_create_pageheader">
        <span id="teach_create_pageheader_back"></span>
        Step 1
        <span id="teach_create_pageheader_forward"></span>
    </div>
</li>
<li>
    <div class="noselect" id="teach_create_pageheader">
        <span id="teach_create_pageheader_back"></span>
        Step 2
        <span id="teach_create_pageheader_forward"></span>
    </div>
<li>
    <div class="noselect" id="teach_create_pageheader">
        <span id="teach_create_pageheader_back"></span>
        Step 3
        <span id="teach_create_pageheader_forward"></span>
    </div>
<li>
1
  • @Daff thanks, don't know why it formats so dumb everytime! Commented Apr 1, 2011 at 0:45

2 Answers 2

3

The element ID should be unique within a single page.

Is this what you wanted? http://jsfiddle.net/haER9/2/

Register your click events like this

$(".button_forward").click(function(){alert("forward");})
$(".button_back").click(function(){alert("back");})
Sign up to request clarification or add additional context in comments.

6 Comments

@Quincy what do I need to do to fix the issue then. I'm not that great with jQuery.
@Quincy yes, but the buttons do not work now. They show up yes, but don't move the pages as they originally did..
because your buttons are having duplicated id, I have changed the button_back and button_forward to be classes. Your event should check for classname instead of id.
@Quincy they are all classes now.. They buttons don't follow out with the click function now.
@JD show us how you bind the click events, or try the way shown in my answer.
|
2

Per jQuery API (see http://api.jquery.com/id-selector/)...

Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM.

I would avoid reusing identifiers and select instead on class.

For example (not sure if this works for you - but you get the idea)...

<li>
    <div class="noselect" id="teach_create_pageheader1">
        <span class="teach_create_pageheader_back"></span>
        Step 1
        <span class="teach_create_pageheader_forward"></span>
    </div>
</li>

$('.teach_create_pageheader_back').prepend($pgBack);
$('.teach_create_pageheader_forward').prepend($pgForward);

6 Comments

@Mayo , how should I clean the code up to do this? You seem better with JQ than I am!
@Mayo I already changed the classes. Now the elemtents show up everywhere, but the buttons do not work.
I made a fiddle for it to post as an answer but I'll just add it here since you got it already jsfiddle.net/mharen/XFCwJ
@Michael Haren , The buttons do not work now..But do show up everywhere!
@jd audi what are you expecting the buttons to do? In my fiddle, I just make them do an alert. You don't have any elements in your markup that are classed '.swShowPage.active' so either that's wrong or you're not providing all the code
|

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.