/    Sign up×
Community /Pin to ProfileBookmark

Checking if a checkbox exists

Anyone know the syntax for checking if a checkbox exists on a form? I have a situation where a form is generated based on DB info. There may or may not be a checkbox on the form. If there is I will need to check it’s state, which I can do, but I need to know how to see if it is even on the page.

Thanks in advance.

  • Olórin
  • to post a comment
    JavaScript

    4 Comments(s)

    Copy linkTweet thisAlerts:
    @CharlesApr 29.2005 — <script type="text/javascript">
    <!--
    function checkboxExists(f) {
    var e, exists = false, i = 0;
    while (e = f.elements[i++]) {if (e.type == 'checkbox') exists = true}
    return exists;
    }
    // -->
    </script>

    <form action="some-script.pl" onsubmit="alert (checkboxExists (this))">
    Copy linkTweet thisAlerts:
    @neil9999Apr 29.2005 — if(document.getElementById("chckbx")!=null){

    alert("checkbox exists!");

    }

    Hope that helps,

    Neil
    Copy linkTweet thisAlerts:
    @UltimaterApr 29.2005 — If you want to figure-out how many checkboxes are in a form:
    [code=html]
    <form name="myForm">
    <input type="checkbox">
    </form>
    <script type="text/javascript">
    var daForm=document.forms["myForm"]
    var checkBoxElements=[]
    for(var i=0;i<daForm.elements.length;i++){
    if(daForm.elements[i].type=="checkbox")
    checkBoxElements[checkBoxElements.length]=daForm.elements[i]
    }
    alert(checkBoxElements.length)
    </script>
    [/code]
    Copy linkTweet thisAlerts:
    @Ol__rinauthorApr 29.2005 — Thanks for the quickness and number of responses. Everything I need is here! ?

  • - Olórin
  • ×

    Success!

    Help @Ol__rin spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.5,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...