Gopesh
Gopesh

Reputation: 3950

How to display a particular word in bold using jquery?

How to display a particular word in bold using jquery.

function OnSuccess(response) {
            var user = $('#username').val();
            if (response.d == 1) {


                $('#result').text(user + ' already Exists');
                $('#Button1').attr('disabled', true);

            } else {
                $('#result').text('Username '+user +' Ok');
                $('#Button1').attr('disabled', false);
            }


        }

Here i want to make the username(user) to be bold ,rest of the sentence must be normal.Is it possible by using jquery?

Upvotes: 6

Views: 2247

Answers (7)

tleilax
tleilax

Reputation: 94

It is possible by changing .text() to .html() and passing a valid chunk of html, e.g.:

$('#result').html('<strong>' + user + '</strong> already Exists');

Upvotes: 2

mathieug
mathieug

Reputation: 911

You can use .css() to set the attribut font-weight to bold. Example : $('#mytext').css('font-weight', 'bold')

Upvotes: 2

Andreas Louv
Andreas Louv

Reputation: 47127

Use html instead of text

function OnSuccess(response) {
    var user = $('#username').val();
    if (response.d == 1) {
        $('#result').html('<b>' + user + '</b> already Exists');
        $('#Button1').attr('disabled', true);
    } else {
        $('#result').html('Username <b>' + user + '</b> Ok');
        $('#Button1').attr('disabled', false);
    }
}

Upvotes: 3

sebbl.sche
sebbl.sche

Reputation: 321

I peronally prefer using a own span-tag to highlight the username.

function OnSuccess(response) {
            var user = $('#username').val();
            if (response.d == 1) {


                $('#result').html('<span id="user">' + user + '</span> already Exists');
                $('#Button1').attr('disabled', true);

            } else {
                $('#result').html('Username <span id="user">'+user +'</span> Ok');
                $('#Button1').attr('disabled', false);
            }

            $('#user').css('font-weight', 'bold');

        }

Upvotes: 3

mindandmedia
mindandmedia

Reputation: 6825

$('#resut').html("Hello " + "<span class='bold'>" + user  +"</span>");

Upvotes: 1

Frederick Behrends
Frederick Behrends

Reputation: 3095

function OnSuccess(response) {
            var user = $('#username').val();
            if (response.d == 1) {


                $('#result').html('<strong>' + user + '</strong> already Exists');
                $('#Button1').attr('disabled', true);

            } else {
                $('#result').html('Username <strong>'+user +'</strong> Ok');
                $('#Button1').attr('disabled', false);
            }


        }

this should do it.

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

Try:


$('#result').html('<strong>'+user+'</strong>' + ' already Exists');

Upvotes: 2

Related Questions