Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Press button to "Bold" & press again to "Unbold"

I am new to javascript. I want to make a button in html using javascript. When user try to press the button the specific <#id> is going to be "Bold" and press again to "Unbold".

How can I do this ? please refer any helping material. Thanks !

like image 754
Noumi Avatar asked Mar 02 '12 05:03

Noumi


People also ask

Is there a shortcut key to change to bold?

Type the keyboard shortcut: CTRL+B.


3 Answers

Since the learning experience is vital, especially for Javascript, I'll guide you in the right direction.

First, you should look into event binding and mouse event handling with jQuery. You can do some powerful stuff with this knowledge, including what you would like to do.

Second, look into basic jQuery selectors. It's not hard to learn the simple CSS-based selectors that you can use to select your desired id.

Third, look at the css() function of jQuery, which falls under jQuery's CSS category. You can use this to set different properties of elements, including font weight.

like image 57
Purag Avatar answered Oct 23 '22 12:10

Purag


I am guessing, you are trying to build something like RTE, So, applying class to give bold effect would be better and efficient solution to this.

It can be done as simple as this

$(".boldtrigger").click(function() { //boldTrigger being the element initiating the trigger
    $(".text").toggleClass("bold");  //text being the element to where applied to it.
});

CSS

.bold { font-weight: bold; }

Demo

like image 40
Starx Avatar answered Oct 23 '22 11:10

Starx


JQuery's toggle() method should take care of this.

$('#foo').toggle(function() {
    $('#bar').css('font-weight', 'bold');
}, function() {
    $('#bar').css('font-weight', 'auto');
});

When you click #foo, it will do the next function in sequence, so this is exactly what you want.

like image 2
Christian Mann Avatar answered Oct 23 '22 11:10

Christian Mann