Question

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.

Was it helpful?

Solution

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

OTHER TIPS

the setAttribute takes 2 parameters name and value:

setAttribute('name','value');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top