0

I want to change css inside function call.ie, in my case add style to check box when function return true. But it is not working.

Script code ,

function checkAll()
{
    if ( document.myForm.myCB.length )
        {
            for (var x = 0; x < document.myForm.myCB.length; x++)
            {
                var flag=1;
                    if (flag == 1)
                    {
                        document.myForm.myCB[x].checked = true; 
                $('.checkbox').attr("style", "background-position:top left;");              
                    }
                    else 
                    {
                        document.myForm.myCB[x].checked = false;
                    }

            }
        }

}

Html code is,

   <form name="myForm" id="myForm" action="" method="get"> 

   <p><input type="button" name="ca_v1_on" value="Check All" onclick="checkAll(1);"/></p>
   <p> <input class="checkbox" type="checkbox" name="myCB" value="yes" id="myCB"/></p>

  <p><input class="checkbox" type="checkbox" name="myCB" value="yes" id="myCB"/></p>

  <p><input class="checkbox" type="checkbox" name="myCB" value="yes" id="myCB"/></p>
 </form>

please help.

thanks,

3 Answers 3

2

use .css for jQuery:

$('.checkbox').css("background-position", "top left");

or simple javascript:

var elements = document.getElementsByClassName('checkbox');
for(var i = 0, length = elements.length; i < length; i++) {
     elements[i].backgroundPosition = 'top left';
}

Note that getElementsByClassName() is not available up to and including IE8.

You can get it working on all browsers, check this for some comparisions an good alternatives like:

if (!('getElementsByClassName' in document)) {
    document.getElementsByClassName = function(className, parentElement) {
        if (Prototype.BrowserFeatures.XPath) {
            var q = ".//*[contains(concat(' ', @class, ' '), ' " + className + " ')]";
            return document._getElementsByXPath(q, parentElement);
        } else {
            var children = ($(parentElement) || document.body).getElementsByTagName('*');
            var elements = [],
                child;
            for (var i = 0, length = children.length; i < length; i++) {
                child = children[i];
                if (Element.hasClassName(child, className)) elements.push(Element.extend(child));
            }
            return elements;
        }
    };
}​
Sign up to request clarification or add additional context in comments.

Comments

0

See this line:

$('.checkbox').attr("style", "background-position:top left;");

You could write it in this way :

$('.checkbox').css("background-position", "top left");

Comments

0

to change css you should use the .css and not the .attr

so this

$('.checkbox').attr("style", "background-position:top left;");

will make

$('.checkbox').css("background-position", "top left");

sported in all browsers with JavaScript if you want to make it different for browsers with out JavaScript take a look at modernizr

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.