javascript - Which is better practice for adding a class using jQuery? -



javascript - Which is better practice for adding a class using jQuery? -

$("#" + parentelementid + " label").attr("class", "disabled")

vs

$('#radiolabel').addclass('disabled');

which pros , cons?

thanks

here advantages of 2 ways this:

addclass("disabled") pros:

if have other classes on object, addclass() preserve while adding new class. pretty much want preserve ability utilize other classes css styling reasons or mutual selector reasons addclass() makes possible add together disabled class without disturbing other classes on same object other reasons. the code reads little more self-explanatory since name addclass() tells reading code it's doing. addclass() automatically manages separators between class names there no accumulation of separators when have multiple class names can occur if current classname , add together onto string manipulation.

attr("class") = "disabled" pros:

if ever want 1 class name on object, 1 statement insures have 1 class name. a direct assignment of 1 class can faster addclass() has examine what's there first , add together class pre-existing classes jquery regex match. max speed element.classname = "disabled" (no jquery @ all).

javascript jquery html

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -