I’m trying to capitalize the first character of a string from user input field. Below is the listener tag that collects the firstname data.
`<script>
(function () {
var element = document.querySelector('input[name="firstname"]')
element.addEventListener("blur", function(e) {
dataLayer.push({
'event': 'first_name',
'first_name': e.target.value
});
}, true);
})();
</script>`
I’ve tried a few variations of the below but none are working
`<script>
(function () {
var element = document.querySelector('input[name="firstname"]');
**var elementUpper = element.charAt(0).toUpperCase() + str.slice(1);**
**elementUpper**.addEventListener("blur", function(e) {
dataLayer.push({
'event': 'first_name',
'first_name': e.target.value
});
}, true);
})();
</script>`
I’ve also created a new tag with the below HTML which isn’t working.
`function() {
var txt = {{dlv - first_name}};
return txt.replace(/wS*/g, function(t){
return t.charAt(0).toUpperCase() + t.substr(1).toLowerCase();
});
}`
Any guidance would be appreciated.
New contributor
user25882303 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.