javascript - cannot change <label> text color -


my research base html dom element says dom element's "style" property (from http://www.w3schools.com/jsref/dom_obj_all.asp):

   "style --    sets or returns style attribute of element" 

the 'label' tag dom element. , has 'style' property. points out on w3schools link above, dom elements have 'style' property.

and in fact, here i'm setting (inline) 'style' property label tag -- , works fine:

    <label for="itemimageid" style="color: gray" id="labelforimageuploadid">item image</label> 

the label text color gray @ page load time.

under condition (user has indicated they're ready select image upload) -- need show upload 'enabled' changing initial gray text color of above black.

do know use css class label's text color , use 'classname' property dynamically alter css class of above? bet do. tonight though i'm holding dom element's feet fire. have 1 'style' attribute change (text color) , don't want add class -- i'm trying here should work according docs.

i want know why can't use 'style' property dom says can -- "get" , "set" dom element's properties.

here i'm "set"-ing 'style' property of -- nothing -- label text remains gray:

    document.getelementbyid('labelforimageuploadid').style = "color: rgb(0,0,0)"; 

nor change color gray black:

    document.getelementbyid('labelforimageuploadid').style = "color: black"; 

the above code executes (in javascript) after label visible on page, , in response onclick event of button on form label part of.

is there bug in ability "set" dom element's 'style' property? according http://www.w3schools.com/jsref/dom_obj_all.asp,

   "htmlelement object      following properties, , methods can used on html elements."          (other properties here.....)    "style --    sets or returns style attribute of element"        (still other properties here......) 

so why can't change element's 'style' property in code above?

upon reviewing answer in 2017, original example of setting whole style string work correctly. don't know problem was, examples below still valid approaches.


setting style javascript follows following format:

document.getelementbyid("abc").style.[css property name in camel case] = "[value]"; 

if using jquery, becomes bit cleaner:

// find elements tag name "bar" direct  // descendants of element class name "foo" $(".foo > bar").css("color", "red");  // set multiple properties $(".foo > bar").css({ color: "red", "background-color": "beige" }); 

Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -