Troy
Troy

Reputation: 99

Javascript arguments as array?

so I have a button with this event:

onmousedown="hideElements('\x22cartview\x22,\x22other\x22')"

and then this function hideElements:

function hideElements(what)
  {
  var whichElements=[what];
  alert(whichElements[0]);
  }

I want it to alert "cartview" but it alerts

"cartview","other"

I am aware of the arguments object but in this case I don't know how to use it to access the individual strings that are comma separated. Probably there is an easy solution but I am kind of new to this. Thanks!

Upvotes: 0

Views: 664

Answers (2)

jay
jay

Reputation: 1524

It looks like the real problem is that you're passing an string, not an array. So you'd do something like:

function hideElements(/* String */ what) {
    alert(what.split(',')[0]);
}

or with an array:

function hideElements(/* Array<String> */ what) {
    alert(what[0]);
}

or passing multiple strings directly into the function:

function hideElements(/* String */ what) {
    alert(arguments[0]);
}

Upvotes: 4

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

onmousedown="hideElements([ 'cartview', 'other' ])"

and then:

function hideElements(what) {
    alert(what[0]);
}

Upvotes: 5

Related Questions