Carasel
Carasel

Reputation: 2871

Change button text to bold on click

I'm trying to make the text of a button become bold when it is clicked on. I have tried using the advice from this question: How to make a text in font/weight style bold in JavaScript

http://jsfiddle.net/14tjwvnq/

function boldButton(){
    $('#btn').style.fontWeight = '700';
}

Upvotes: 2

Views: 9603

Answers (3)

d.raev
d.raev

Reputation: 9546

YOU MIGHT NOT NEED JQUERY

//Inline
<button class="btn" id="btn1" onclick="this.style.fontWeight = 'bold'">Test1</button>
//OR with function
<button class="btn" id="btn2" onclick="boldButton(this)">Test2</button>

<scrypt>
function boldButton(btn){
    btn.style.fontWeight =  '700';
}
</scrypt>

fiddle

Upvotes: 2

Milind Anantwar
Milind Anantwar

Reputation: 82241

You need to convert jquery object to javascript object before setting the property through javascript. also make sure that onclick method is defined :

 function boldButton(num){
   $('#btn'+num)[0].style.fontWeight = '700';
 }

Working Demo

Upvotes: 0

antyrat
antyrat

Reputation: 27765

In jQuery you need to use css method:

$('#btn').css( 'font-weight', '700' );

jsFiddle

Upvotes: 4

Related Questions