Evan
Evan

Reputation: 3511

Get the value of array

I'm trying to get the value of the array based on the URL I can insert the value hello-world/ or hello-everybody/ based on the URL location into my link.

var loc = window.location.pathname;
var myArray = new Array( "hello-world/", "hello-everybody/" );
var isFound = false; 

for(var i=0,len=myArray.length;i<len;i++) {
  if(loc.indexOf(myArray[i])> -1) {
     isFound = true;
     break;
  }
} 

if(isFound) {
alert("href='//www.example.com/"+arrayvaluehere+"path/index.html")

}
});

I thought I could use this but I get this error TypeError: b0.nodeName is undefined

var arrayvaluehere = loc[$(myArray).val() - 1]

Upvotes: 0

Views: 30

Answers (1)

Rik Lewis
Rik Lewis

Reputation: 749

The reason you're getting an error is because loc is a string, not an array. Also .val() returns a string, and therefore you can't -1 from it.

Based on this, it's hard to tell what the answer is. But I think maybe you could set isFound = -1 initially and then isFound = i in the loop. Then the last bit would be...

if(isFound>-1) {
  alert("href='//www.example.com/"+myArray[i]+"path/index.html");
}

Upvotes: 1

Related Questions