I have this JavaScript function:
function Test(isValid) {
var divStart = $get('divDateFrom');
var divEnd = $get('divDateTo');
var txtStartDate = divStart.firstChild;
var txtEndDate = divEnd.firstChild;
var isValidFromForecastStartDate;
txtEndDate.setAttribute('dateInRegionalFormat', txtEndDate.value);
}
This function is working fine in IE but I'm getting "txtEndDate.setattribute
is not a function" error in Firefox and Chrome.
The "setAttribute is not a function" error occurs for multiple reasons: calling the setAttribute() method on a value that is not a DOM element. placing the JS script tag above the code that declares the DOM elements. calling the setAttribute method on a jQuery object (should use attr() instead).
Element.setAttribute() Sets the value of an attribute on the specified element. If the attribute already exists, the value is updated; otherwise a new attribute is added with the specified name and value. To get the current value of an attribute, use getAttribute() ; to remove an attribute, call removeAttribute() .
Another way to change or create an attribute is to use a method like element. setAttribute("attribute", "value") or element. createAttribute("attribute", "value"). Use setAttribute to change an attribute that has been defined before.
JavaScript setAttribute() The setAttribute() method is used to set or add an attribute to a particular element and provides a value to it. If the attribute already exists, it only set or changes the value of the attribute. So, we can also use the setAttribute() method to update the existing attribute's value.
Use jquery.attr() like,
$(txtEndDate).attr('dateInRegionalFormat', txtEndDate.value);
Updated there may be multiple elements
so use [0]
for the first element
like,
txtEndDate[0].setAttribute('dateInRegionalFormat', txtEndDate.value);
You should first check whether the elements exists or not
before setting attribute
in it like,
if(txtEndDate.length)
{
txtEndDate.setAttribute('dateInRegionalFormat', txtEndDate.value);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With