0

I wanted to know how can we retrieve the type of the element with the tag name I am creating a code on a HTML form. There are all types of HTML input elements (textfield, button, datepicker).

What I want is that if the user selects a option from a drop down, disable the checkboxes. But the checkbox are generated using PHP loop and I have also passed id in the checkbox name, so I cannot use:

document.getElementsByName('checkbox');

The name of my checkbox is "checkbox", so i tried using:

document.getElementsByTagName('input');

which i for disabling the checkbox but this disabled all the input element on the page. Is there any solution for this? How I could use the type attribute with it as checkbox and disable on checkbox:

function  disabler()
{  
     checkboxes = document.getElementsByTagName('input');
     if(checkboxes.type='checkbox')
     {
        for(var i=1, n=checkboxes.length;i<n;i++)
        {
            checkboxes[i].disabled = true;//here i want to disable only the checkbox on page 
        }
 }
}
2
  • 2
    Please take the time to format your code readably when asking for help. Commented Oct 26, 2013 at 11:54
  • okkk thnk i will keep this in mind Commented Oct 26, 2013 at 12:00

3 Answers 3

3

You're using assignment operator = NOT comparison ==. Replace this line

if(checkboxes.type='checkbox')

with

if(checkboxes.type == 'checkbox')

or a strict comparison operator === (compares also variable type, e.g. "0" === 0 is false but "0" == 0 is true)

if(checkboxes.type === 'checkbox')

Also you're trying to access type attribute of a collection returned by getElementsByTagName. To access specific element refer to checkboxes as it was an array

if(checkboxes[0].type === 'checkbox') //accesses first checkbox element

To loop through all elements while checking each one if it is a checkbox do this

for(var i=0; i < checkboxes.length; i++)
    {
        if(checkboxes[i].type === 'checkbox') {
            checkboxes[i].disabled = true;
        }
    }

Here's an example fiddle for you

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

5 Comments

You're right that = is the wrong operator, but testing checkboxes.type is also wrong in that checkboxes is a list...
ya i tried if(checkboxes.type === 'checkbox') but it didnt worked...!
@MeeneshJain, I edited the answer. See the last line of code.
sir i used === it makes my code non working ..... only = works fine but it disables all input on that page
Sorry, I had to update it again. Please check it now. Use the last block of code (6 lines).
2

In JavaScript, = is an assignment operator. == and === are the equality operators (loose and strict, respectively). So:

if(checkboxes.type=='checkbox')  // Loose, does type conversion (not relevant here)

or

if(checkboxes.type==='checkbox') // Strict, no type conversion (not relevant here)

Comments

2

checkboxes is a Node List, not a single element. You need to do the test on checkboxes[i]:

function  disabler()
{  
    checkboxes = document.getElementsByTagName('input');
        for(var i=1, n=checkboxes.length;i<n;i++)
        {
            if (checkboxes[i].type == 'checkbox') {
                checkboxes[i].disabled = true;
            }
        }
}  

Comments

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.