Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make text bold,italic and underline using jquery

Tags:

jquery

css

I have three checkboxes and a textbox now If I write something in textbox and check the bold checkbox the text should appear with bold effect and similarly italic and underline without postback(i.e it should reflect immediately with the selected effect).

Here is my code:

Bold:<input type="checkbox" value=""/>
Italic:<input type="checkbox" value=""/>
Underline:<input type="checkbox" value=""/>

<input type="text" value="">
like image 378
coder Avatar asked Oct 30 '11 16:10

coder


People also ask

How do you underline text in jQuery?

Given a statement and the task is to underline all the text words of HTML page using jQuery. We will use text-decoration property to add underline to each words.

How do I make bold text italics underline?

select the word or phrase and use the following shortcuts ctrl+u (underline), ctrl+b (bold), ctrl+i (italic) or use the icons on the home ribbon.

How do I make the first text bold in jQuery?

To make the first word bold, we use html(), and text() methods. html() Method: The html() method in jQuery is used to set or return the innerHTML content of the selected element. Syntax: It sets the content of matched element. text() Method: This method is used to set or return the text content of the element.


2 Answers

You can do the following:

<form>
    Bold:<input name="textStyle" type="checkbox" value="bold"/>
    Italic:<input name="textStyle" type="checkbox" value="italic"/>
    Underline:<input name="textStyle" type="checkbox" value="underline"/>

    <input name="styledText" type="text" value="">
</form>

<script type="text/javascript">
    $('input[name="textStyle"]').change(function(){
        if ($(this).val() == 'bold'){
            if ($(this).is(':checked')) $('input[name="styledText"]').css('font-weight', 'bold');
            else $('input[name="styledText"]').css('font-weight', 'normal');
        }
        else if ($(this).val() == 'italic'){
            if ($(this).is(':checked')) $('input[name="styledText"]').css('font-style', 'italic');
            else $('input[name="styledText"]').css('font-style', 'normal');
        }
        else if ($(this).val() == 'underline'){
            if ($(this).is(':checked')) $('input[name="styledText"]').css('text-decoration', 'underline');
            else $('input[name="styledText"]').css('text-decoration', 'none');
        }
    });
</script>

Fiddle here: http://jsfiddle.net/tyfsf/

Hope it helps!

like image 117
dSquared Avatar answered Sep 20 '22 06:09

dSquared


You can do that using simple CSS and a little jQuery code.

1.First define your cascading style sheet classes

<style type="text/css">
.bold {
 font-weight:bold;
}
.italic {
font-style:italic;
}
.underline {
    text-decoration: underline;
}
</style>

2.Load jQuery

<script type="text/javascript" src="jquery.js"></script>

3.Write the function for switching the classes

<script type="text/javascript">
$(document).ready(function () {
    $('.selector').click(function () {
        var checked = $(this).is(':checked');
        var value = $(this).attr('value');
        if(checked) {
            $('#box').addClass(value);
        } else {
            $('#box').removeClass(value);
        }
    });     

});
</script>

5.Modified html

Bold:<input class='selector' type="checkbox" value="bold"/>
Italic:<input class='selector' type="checkbox" value="italic"/>
Underline:<input class='selector' type="checkbox" value="underline"/>
<br>
<input id="box" type="text" value="">
<br>

jsfiddle link: http://jsfiddle.net/deepumohanp/t2wKP/

like image 39
WarFox Avatar answered Sep 20 '22 06:09

WarFox