I have a JS function activating after an onclick event on an A tag. Here is the code :
(function(MTM) {
MTM.selectAllNone = function(e) {
var string = e.target.textContent || e.srcElement.innerText;
var list = document.getElementById('my-div');
var inputs = list.getElementsByTagName('input');
var length = inputs.length;
if (string == 'Select all') {
for (var i=0; i<length; i++) {
if (inputs[i].getAttribute('type') == 'checkbox') {
inputs[i].setAttribute('checked', 'checked');
}
}
string = 'Select none';
}
else {
for (var i=0; i<length; i++) {
if (inputs[i].getAttribute('type') == 'checkbox') {
inputs[i].removeAttribute('checked');
}
}
string = 'Select all';
}
};
}(window.MTM = window.MTM || {}));
And on the A tag, here it goes :
<a href="javascript:void(0);" onclick="MTM.selectAllNone(event);">Select all</a>
Now, while debugging, I can see that “string” contains the text “Select all”. It goes well through the if (string == 'Select all'). And the line string = 'Select none'; does change the string variable. But it doesn’t change the text of the anchor tag.
Maybe something obvious?
var string = e.target.textContentdoes not mean thatstringbecomes an alias fore.target.textContent, it means that thestringpoints to the same value. When you dostring = "blah"you are just pointingstringat a different value. You need to point the property.textContentofe.targetat the value new value you want.