hellomello
hellomello

Reputation: 8585

multiple function selectors when clicked?

I'm still learning how to write functions..

I want to be able to click on an item, and it will add onto a function

<a href='#' onclick="play('url1')">Item 1</a>
<a href='#' onclick="play('url2')">Item 2</a>
<a href='#' onclick="play('url3')">Item 3</a>

So if I'm trying to access any of these items, one, two or even all three at the same time, how do I call it into a function if I have something like this

function play(url_src){

}

src would only be using one item at a time? is there an array associated to this?

do I have to do something like this

function play(url_src[]){

}

Upvotes: 0

Views: 83

Answers (2)

longbkit
longbkit

Reputation: 1298

You could either define a new function as mentioned or try onclick="play('url1');play('url2')"

Upvotes: 0

Blender
Blender

Reputation: 298106

Make your parameter an array:

<a href='#' onclick="play(['url1', 'url2'])">Item 4</a>

And parse it as such:

function play(url_src){
  if (!(url_src instanceof Array)) {
    url_src = [url_src];
  }

  for (var i = 0; i < url_src.length; i++) {
    url = url_src[i];

    // Play each url.
  }
}

I use the if statement on the second line to account for the parameter not being an array, like so:

<a href='#' onclick="play('url1')">Item 5</a>

Upvotes: 1

Related Questions