I have a list of links that direct to a page which corresponds to each of the 50 states in the US.
I am looping through each states in an array and adding it the href for each but for states that contain two words I need to replaces the space (' '
) between them with a hyphen('-'
).
example: New York
must be replaced with New-York
.
<a href="http://www.projectknow.com/find/New York/" class="spaces">
MUST BE REPLACED WITH
<a href="http://www.projectknow.com/find/New-York/" class="spaces">
This is what I got so far but it does NOT work. Still new to jQuery and any help would be greatly appreciated.
var option = '';
for (var i = 0; i < states.length;i++){
option += '<li><a href="http://www.states.com/find/'+ states[i] +'/"
class="spaces">'+ states[i] +'</a></li>';
}
$('.menu').append(option);
$("a.spaces").each(function (){
$(this).attr('href', $(this).attr("href").replace("","-"));
});