admin管理员组

文章数量:1315807

I'm trying to make the text of a button bee 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

/

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

I'm trying to make the text of a button bee 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/14tjwvnq/

function boldButton(){
    $('#btn').style.fontWeight = '700';
}
Share Improve this question edited May 23, 2017 at 12:10 CommunityBot 11 silver badge asked Jan 6, 2015 at 13:02 CaraselCarasel 2,8715 gold badges33 silver badges53 bronze badges 3
  • what should be the behavior when you click it again ? do you need the Bold to be temporary (sec, while holding the mouse down), to taggle on each click , or just to stay bolded after the first click. – d.raev Commented Jan 6, 2015 at 13:06
  • I need it to stay bold after the first click. – Carasel Commented Jan 6, 2015 at 13:21
  • your example gets error "ReferenceError: boldButton is not defined" as the function is not defined on after the HTML is rendered. Move the function in the header, or use some selector to assign the ONCLICK to the buttons onLoad. – d.raev Commented Jan 6, 2015 at 13:27
Add a ment  | 

3 Answers 3

Reset to default 4

In jQuery you need to use css method:

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

jsFiddle

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

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

本文标签: javascriptChange button text to bold on clickStack Overflow