31

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

1
  • You can set class attributes to "btn brown selected" and not style attributes. Commented May 21, 2022 at 6:55

4 Answers 4

51

Use the CSS function from jQuery to set styles to your items :

$('#buttonId').css({ "background-color": 'brown'});
Sign up to request clarification or add additional context in comments.

2 Comments

better answer, imho. More cleanly articulated without the editorial commentary. +1
Another example to change background as well as text color: $('#MySpanTag').css({ "background-color": 'orange', "color": 'red' }); span in asp.net mvc razor cshtml: <span id="MySpanTag" style="color:Green"> @ViewBag.MySpanTagMessage </span>
40

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?

Comments

2
$("span").mouseover(function () {
$(this).css({"background-color":"green","font-size":"20px","color":"red"});
});

<div>
Sachin Tendulkar has been the most complete batsman of his time, the most prolific     runmaker of all time, and arguably the biggest cricket icon the game has ever known. His batting is based on the purest principles: perfect balance, economy of movement, precision in stroke-making.
</div>

1 Comment

Thanks for posting your answer! Please be sure to read the FAQ on Self-Promotion carefully. Also note that it is required that you post a disclaimer every time you link to your own site/product.
0

The question was misleading by confusing the html style attribute and css attributes.

However, if you want to actually modify the style attribute of an html element using jQuery, just reassign the entire style attribute like this:

<div id="mydiv" style="position:absolute; right:0; display:block;">
</div>

<script>
    // change position to relative and remove "right" and "display" css attributes
    $('#mydiv').attr("style", "position:relative;");
</script>

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.