5

I have a problem with set fontStyle attribute to single element(by id) in popover. I get onClick event, put style (bold font style) to all class elements and then try to put another style(normal) to one(clicked) element. My code is:

$(document).on('click', '.listElement', function () {
    $('.listElement').css('font-weight', 'bold');
    alert(this.id);
    $(this).css('font-weight', 'normal');
});

There is a demo

Setting bold style for all class elements works and dialog shows correctly id so this is right element(in this) but .css() method doesn't work. I tried to put style by using id like:

$('#A').css('font-weight', 'normal');

and there is no error in console but it doesn't work as well. Is there any other way to set fontStyle to single unique element?

5
  • 1
    Your code is actually working. But when the popover closes on click on it or outside and opens again the plugin re initialises the font to the initial state. Commented Apr 16, 2017 at 18:52
  • @DanPhilip I'm not sure but probably you are wrong because there is not initial state. In page load is normal font style for all elements and after first click(on any element) there is bold style for all. Commented Apr 16, 2017 at 18:57
  • the problem is, you redirect to your page every time you click on a link and the complete code runs from begin. if you want to save the click state you have to store it in sessionStorage and request on script begin. try it without links and you will see the css works. another option is to write a single side application with MVC and listening on hashChange event. (I'm working on a tutorial for that) Commented Apr 16, 2017 at 19:02
  • @TypedSource so why set font style for elements by class works? Commented Apr 16, 2017 at 19:07
  • @barmi because you don't reload your page Commented Apr 16, 2017 at 19:08

1 Answer 1

2

Rather than removing CSS styling why not just never add it to the clicked object??

Get the ID of the clicked object and use that...

$(document).on('click', '.listElement', function () {
var clickEl = this.id; //ID of clicked object
$('.listElement').not('#' + clickEl).css('font-weight', 'bold'); //bolds everything which is NOT the clicked item.
alert(this.id);
});

or

$(document).on('click', '.listElement', function () {
$('.listElement').not('#' + this.id).css('font-weight', 'bold'); //bolds everything which is NOT the clicked item.
alert(this.id);
});

Updated Fiddle


if there is a specific reason you need to remove the styling rather than never applying it...

$(document).on('click', '.listElement', function () {
$('.listElement').css('font-weight', 'bold');
alert(this.id);
$('.listElement#' + this.id).css('font-weight', 'normal');
});

Fiddle for that

Sign up to request clarification or add additional context in comments.

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.