I got 3 buttons-links triggering some javascript code, with indication if certain button is selected, selected button got style attribute set to
"btn brown selected"
while other buttons got this attribute set to
"btn brown"
only one button can be selected at one time, each of buttons got different unique id, my question is how using jquery access certain button to modify it's style attr. What i need its only how to access by id single button and modify its style attribute to get immediate update on the screen
thanks in advance MTH
To change the id attribute of an HTML element, you can use the jQuery attr() method which allows you to set an element's attribute value. You can change any HTML element id attribute value by using the example above as a reference.
The css() method is used to change style property of the selected element.
id is not a valid jquery function. You need to use the . attr() function to access attributes an element possesses.
Use the CSS function from jQuery to set styles to your items :
$('#buttonId').css({ "background-color": 'brown'});
Not sure I completely understand the question but:
$(":button.brown").click(function() { $(":button.brown.selected").removeClass("selected"); $(this).addClass("selected"); });
seems to be along the lines of what you want.
I would certainly recommend using classes instead of directly setting CSS, which is problematic for several reasons (eg removing styles is non-trivial, removing classes is easy) but if you do want to go that way:
$("...").css("background", "brown");
But when you want to reverse that change, what do you set it to?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With