Reputation: 2694
I use a form that display some div depending on which option you click of the radio button.
The thing is that the div is set to be none displayed, except if I select an option.
So I added the following function in order to make sure that if the div is displayed it will have th form havin the required property.
SO I give this function:
<script type="text/javascript">
function addattribute()
{
if (document.getElementById('montant').style.display == "block")
{
document.getElementsByName('montant_creance').setAttribute('required');
document.getElementsByName('condition2').setAttribute('required');
}
if (document.getElementById('montant').style.display == "none")
{
document.getElementsByName('montant_creance').setAttribute('required','false');
document.getElementsByName('condition2').setAttribute('required','false');
}
}
</script>
But It say to me in the console:
Uncaught TypeError: Object #<NodeList> has no method 'setAttribute'
I really do not know how to find an issue.
Receive all my utmost Respect.
Kind Regards.
SP.
Upvotes: 0
Views: 2347
Reputation: 123407
getElementsByName
return a collection of elements, so you have to write e.g.
document.getElementsByName('montant_creance')[0].setAttribute('required');
every time you use this method (or similar methods, like getElementsByTagName
, getElementsByClassName
...) using the right index
Upvotes: 2
Reputation: 1716
the setAttribute takes 2 parameters name and value:
setAttribute('name','value');
Upvotes: 0