SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    396
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Getting the document's form name!?

    Hi,

    I want to loop through *every* input on a page, check if the input is enabled, and if so, change the background colour. At the moment I have this:

    Code Javascript:
    function checkenabled () {
    	var elem = document.form.elements;
    	for(var i = 0; i < elem.length; i++)
    	{
    		if (elem[i].disabled = false) { elem[i].backgroundcolor = grey }
    	} 
    }
    It isn't working, and the page says that document.form isn't an object. Problem is, all of the forms on various pages (around forty) have a different form name, so defining it manually isn't going to work, and I'd like to keep the Javascript in one place if at all possible.

    Cheers

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are at least three problems with that code.
    1. There's no document.form property.
    2. You're assigning instead of comparing in the if-statement.
    3. The syntax for setting the colour is wrong.


    Try something like this,
    Code JavaScript:
    function checkenabled() {
        var forms = document.getElementsByTagName("form");
        for (var f = 0, fc = forms.length; f < fc; ++f) {
            var elem = forms[f].elements;
            for (var e = 0, ec = elem.length; e < ec; ++e) {
                if (!elem[e].disabled) elem[e].style.backgroundColor = "gray";
            }
        }
    }
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    396
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Blimey, many thanks AutisticCuckoo; I made a bit of a mess of that! Thanks for letting me what I did wrong too, really appreciate it. Thanks again

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm glad I could help.
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •