1

My Exlanation of what I am trying to do, (tried explaining it in 2 ways to help you get an idea)

example 1

  1. store all elements like so: <a href="etc">url1</a> from inside <div class=".resources"></div> in an array, so that they could be clicked with click(); function
  2. open first url from array with default button - i.e. $(array[0]).click();
  3. open next url from array with next button - i.e. last clicked $(array[+1]).click();
  4. open previous url from array with prev button - i.e. last clicked $(array[-1]).click();

example 2

  1. create an empty class in HTML like so: <div class="resources"></div>
  2. create an array of URLs in JAVASCRIPT/JQUERY and store them within .resources class
  3. create prev , default , next buttons in HTML
  4. default button when clicked opens first URL from array i.e.: $(array[0]).click();
  5. next button when clicked opens next URL from array i.e.: $(array[+1]).click();
  6. prev button when clicked opens previous URL from array i.e.: $(array[-1]).click();

MUSTS: urls must be opened with: click(); function: like so: $(array[0]).click();

fiddle to make it clearer: view fiddle

Teallaair9
  • 39
  • 9
  • So, what is the problem that you are having then? – Xotic750 Aug 31 '14 at 20:08
  • i am having problem bringing it to reality, literally spent the past 8 hours trying to make it work and can't – Teallaair9 Aug 31 '14 at 20:09
  • `array[-1]` wont work as array's do not have a negative index, you would need to keep a index variable and manipulate that – Patrick Evans Aug 31 '14 at 20:11
  • So basically you don't know how to code a ring or circular buffer in javascript? Here is one example but there are other references here on SO. http://stackoverflow.com/questions/22174549/how-to-make-back-and-next-buttons-for-images-and-or-videos-in-javascript/22175919#22175919 – Xotic750 Aug 31 '14 at 20:48
  • ya that's what I am aiming to do, but not with videos, rather with URLs. I am using a soundcloud api, which takes a link and plays it when it's clicked. that's why I posted the above example with of what I am aiming for with URLs. All the URLs must be stored within .resources class, and when they're clicked audio is played. Rather then having all URLs shown, I want to have just buttons showing, which would go to next url, if that makes sense – Teallaair9 Aug 31 '14 at 20:59
  • The `RingArray` constructor that I pointed you at can handle any array-like object, so just create a new object using the result of your query and set your click handlers appropriately, using the methods provided by `RingArray`. – Xotic750 Aug 31 '14 at 21:06
  • will try, gonna report back in an hour or so – Teallaair9 Aug 31 '14 at 21:08
  • what your code does is insert a src attribute into the iframe. Whereas, I am trying to add this: inside .resources div. I have no success after 2 hours of trying – Teallaair9 Aug 31 '14 at 23:39
  • I know what that examples ancillary code does, but that is not part of the constructor `RingArray`. It should take about <5 minutes to use `RingArray` with your example. – Xotic750 Sep 01 '14 at 08:48

2 Answers2

1

I modified your fiddle a bit and it seems to do what you want; if I understood it correctly:

modified fiddle

var links = $( '.resources a[href*="url"]' );

var currentLink = 0;

/* click and open default link ! */ 
$('.open-current-link').on('click', function(){ 
    $( links[currentLink] ).click();        
});

/* click and open next link */ 
$('.open-next').on('click', function(){  
    currentLink = currentLink + 1;
    if (currentLink == 4){
        currentLink = 0;
    }
    alert('clicking ' + $(links[currentLink]).attr('href'));
    $( links[currentLink] ).click();        
});

/* click and open previous link */
$('.open-prev').on('click', function(){
    currentLink = currentLink - 1;

    if (currentLink == -1){
        currentLink = 3;
    }

    alert('clicking ' + $(links[currentLink]).attr('href'));
    $( links[currentLink] ).click();      
});
artm
  • 8,554
  • 3
  • 26
  • 43
1

To show show you how simple it is to impliment RingArray for use with your code example.

CSS

body {
    color: #fff;
}
.link {
    width: 180px;
    height: 20px;
    padding: 10px;
    background-color: black;
    cursor: pointer;
    text-align: center;
}

HTML

<div class="resources">
    <a href="url1"></a>  
    <a href="url2"></a>  
    <a href="url3"></a>  
    <a href="url4"></a>    
</div>

/* index -= 1 */
<div class="link open-prev">click previous link</div> 
/* indexed [0] */
<div class="link open-current-link">click default link</div> 
/* index += 1 */
<div class="link open-next">click next link</div> 

Javascript

function RingArray(object, position) {
    this.array = RingArray.compact(object);
    this.setPosition(position);
}

RingArray.toInt32 = function (number) {
    return number >> 0;
};

RingArray.toUint32 = function (number) {
    return number >>> 0;
};

RingArray.isOdd = function (number) {
    return number % 2 === 1;
};

RingArray.indexWrap = function (index, length) {
    index = RingArray.toInt32(index);
    length = RingArray.toUint32(length);
    if (index < 0 && RingArray.isOdd(length)) {
        index -= 1;
    }

    return RingArray.toUint32(index) % length;
};

RingArray.compact = (function (filter) {
    var compact;

    if (typeof filter === 'function') {
        compact = function (object) {
            return filter.call(object, function (element) {
                return element;
            });
        };
    } else {
        compact = function (object) {
            object = Object(object);
            var array = [],
                length = RingArray.toUint32(object.length),
                index;

            for (index = 0; index < length; index += 1) {
                if (index in object) {
                    array.push(object[index]);
                }
            }

            return array;
        };
    }

    return compact;
}(Array.prototype.filter));

RingArray.prototype = {
    setPosition: function (position) {
        this.position = RingArray.indexWrap(position, this.array.length);

        return this;
    },

    setToStart: function () {
        return this.setPosition(0);
    },

    setToEnd: function () {
        return this.setPosition(this.array.length - 1);
    },

    setRandom: function () {
        return this.setPosition(Math.floor(Math.random() * this.array.length));
    },

    increment: function (amount) {
        return this.setPosition(this.position + (RingArray.toInt32(amount) || 1));
    },

    previousElement: function () {
        return this.array[RingArray.indexWrap(this.position - 1, this.array.length)];
    },

    currentElement: function () {
        return this.array[this.position];
    },

    nextElement: function () {
        return this.array[RingArray.indexWrap(this.position + 1, this.array.length)];
    }
};

/* get urls and store in an array */
var links = $('.resources a[href*="url"]'),
    linkRing = new RingArray(links);

/* click and open default link ! */
$('.open-current-link').on('click', function () {
    console.log(linkRing.setToStart());
});

/* click and open next link */
$('.open-next').on('click', function () {
    console.log(linkRing.increment(1));
});

/* click and open previous link */
$('.open-prev').on('click', function () {
    console.log(linkRing.increment(-1));
});

On jsFiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • when i used the console.log it logged the correct URL however, nothing else happens. I believe that it needs to be able to click the whole anchor element, because only then the song playssong name when I click this myself it only works then. – Teallaair9 Sep 01 '14 at 13:04
  • Sure, you have to add the `.click()` code to the code that I have demonstrated, I just logged the output for you to see that the `RingArray` works, you need to do something like `$(linkRing.increment(1).currentElement()).click()` or whatever you want to do with the information stored at the `currentElement`. – Xotic750 Sep 01 '14 at 13:15
  • If you don't know how to dispatch a click event, then that's another question, and there are many examples here on SO. Here is an example using pure modern Javascript http://stackoverflow.com/questions/24711051/auto-click-script-within-a-browser/24712697#24712697 But I'm sure you can also find examples using jQuery if that is what you want. – Xotic750 Sep 01 '14 at 13:39
  • I do know, and I had it added. Just forgot to post it to this comment. I also added an alert function and the correct URL is clicked however, for some reason the song doesn't change. Check this fiddle I just made, that's what I am trying to do, but with controls 'next' 'prev' 'current' http://jsfiddle.net/zctgyf88/ – Teallaair9 Sep 01 '14 at 13:51
  • I don't see a link to your current fiddle, with the `RingArray` code that I posted in my answer? – Xotic750 Sep 01 '14 at 13:54
  • jsfiddle.net/zctgyf88 this is the idea, and because when you click on a link it starts playing I thought that I could implement this URL changer and the controls would play the next song previous etc – Teallaair9 Sep 01 '14 at 14:04
  • Yes, that is the link that I went to, but I don't see any Javascript other than libraries being loaded, on that fiddle. – Xotic750 Sep 01 '14 at 14:06
  • Well, to begin with you have changed the class in the HTML from `resources` to `post` but not changed the jQuery selector to reflect this HTML change. – Xotic750 Sep 01 '14 at 14:15
  • You are also inluding `jquery.min.js` v1.4.2 as an external library, aswell as jQuery v1.9.1 from the menu and from the `console` it shows `Uncaught TypeError: undefined is not a function` as 1.4.1 is being loaded after 1.9.1 and doesn't have a method `on` Or so it seems – Xotic750 Sep 01 '14 at 14:21
  • the github example uses jquery 1.4 only, however, 1.9 files are available for download and I am using them on my own machine. just don't have a cdn to upload them to fiddler: check this they're here: https://github.com/soundcloud/soundcloud-custom-player i am using: sc-player-minimal – Teallaair9 Sep 01 '14 at 14:30
  • I'm sorry but I don't have the time to look into the `soundcloud` API or files. What I can say is that the `RingArray` works as expected (demonstrated in my answer) and from that you can then dispatch an event on the `currentElement`. what I can say is that `soundcloud` is using the deprecated `live` method, so if you are going to use 1.4.1 then you will need to change `on` to `click` (or something like that) in your code. Something like http://jsfiddle.net/Xotic750/xx05mn6e/6/ – Xotic750 Sep 01 '14 at 14:44
  • but if the tracks play from inside the .post class only, it should somehow be possible to control them, maybe have only one URL by default and others stored in an array and replace the value of href attribute with the controls. i.e. 'next' button puts next url from array – Teallaair9 Sep 01 '14 at 14:47
  • We are way off topic from your original problem now, which was how to implement a ring array in Javascript. – Xotic750 Sep 01 '14 at 14:50
  • it's still on topic because we're talking about replacing URL's with URL's from an array. Which we are close to doing now. I believe that what I mentioned in prior reply would work 100% – Teallaair9 Sep 01 '14 at 14:53
  • The `RingArray` constructor will accept any array-like object, be it an object that looks like an array, a real array, a jQuery object, a DOM NodeList etc etc. What you do with the data at the current indexed `RingArray` element is up to you. So an array of URLs (strings) will be fine for the `RingArray`. – Xotic750 Sep 01 '14 at 14:59
  • http://jsfiddle.net/sab60wff/ this fiddle works fine now, url changes with controls. however, could you alter it so that the utubeFrame is a CLASS. Then i will accept it as a solution, thanks for your time so far – Teallaair9 Sep 01 '14 at 15:07
  • Sure, change the HTML `id` to `class` and then `utubeFrame = $('.utubeframe')` and `utubeFrame[0].href = ...` instead of `utubeFrame.href = ...` should do it, I believe. – Xotic750 Sep 01 '14 at 15:25
  • ok thanks accepted as solution, what difference does the [0] make? – Teallaair9 Sep 01 '14 at 16:32
  • so I tried this 'console.log($("a").attr("href"));' on 'random' button and it displays the same url everytime. it doesn't update it – Teallaair9 Sep 01 '14 at 19:53
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/60415/discussion-on-answer-by-xotic750-jquery-store-urls-into-an-array-and-open-them-u). – Taryn Sep 01 '14 at 20:54