I am trying to replace a hyphen in all attributes
<a href="/page" id="someId" data-country="north-america" data-state="north-dakota">North Dakota</a>
like so:
var el = document.getElementById('someId');
Array.prototype.slice.call(el.attributes).forEach(function(item) {
item.value.replace('-','_');
console.log(item.value);
});
Can't quite figure out why it's not actually replacing the hyphen with an underscore. Am I missing something?
String.prototype.replace()
returns a new String, it does not alter the original String. Just assing the new value and you're done.
item.value = item.value.replace('-','_');
Quoted from the MDN documentation:
This method does not change the String object it is called on. It simply returns a new string.