0

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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot, I'put what you told me; and now it say to me An invalid form control with name='condition2' is not focusable.
if you feel this issue is not related with your original question you should open a new discussion and post some code to see in a demo page /fiddle/jsbin otherwise just post a link with some code
in fact it works except that When I click back to the first option and the div is set to not be displayed it show this message.
0

the setAttribute takes 2 parameters name and value:

setAttribute('name','value');

3 Comments

Dear Sir that is what I wrote setAttribute('required','false');
<script type="text/javascript"> function addattribute() { if (document.getElementById('montant').style.display == "block") { document.getElementsByName('montant_creance')[0].setAttribute('required'); document.getElementsByName('condition2')[0].setAttribute('required'); } if (document.getElementById('montant').style.display == "none") { document.getElementsByName('montant_creance')[0].setAttribute('required','false'); document.getElementsByName('condition2')[0].setAttribute('required','false'); } } </script> I have this
<script type="text/javascript"> function addattribute() { if (document.getElementById('montant').style.display == "block") { document.getElementsByName('montant_creance')[0].setAttribute('required','true'); document.getElementsByName('condition2')[0].setAttribute('required','true'); } if (document.getElementById('montant').style.display == "none") { document.getElementsByName('montant_creance')[0].setAttribute('required','fals‌​e'); document.getElementsByName('condition2')[0].setAttribute('required','false'); } } </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.