user1907849
user1907849

Reputation: 990

Changing style inside jQuery selector dynamically

I have a div and I am trying to display a text with bold font as follows:

$('#div').text("data between "+ fromdate + "to" + todate).css({'text-align':'centre'});

How do I display only "fromdate" and "todate" in bold font?

Upvotes: 3

Views: 68

Answers (3)

Ankit Kathiriya
Ankit Kathiriya

Reputation: 1231

You should try this.

$('#div').html("data between "+ "YourText1" + "to" + "YourText2").css('text-align','center');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div id="div"></div>

Upvotes: 1

gurvinder372
gurvinder372

Reputation: 68433

set as html then assign and filter as class, finally set the css to the filtered result

$('#div').html("data between <span class='bold'>"+ fromdate + "</span>to<span class='bold'>" + todate + "</span>").css({'text-align':'center'}).filter( ".bold" ).css( "font-weight", "bold" );

Upvotes: 2

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

Use .html(), and wrap the text in <b> (if it is presentational) or <strong> tags:

$('#div')
    .html("data between <b>" + fromdate + "</b> to <b>" + todate + "</b>")
    .css("text-align", "center");

And centre should be center.

Upvotes: 4

Related Questions