7

I would like to be able to control the font-weight of text if bracketed inside a p tag using JavaScript.

For instance: The cow jumped over the {moon}. font-weight within {} would be increased.

This so the end user can type this into a text area and on submit the would print to page altering the font-weight within the braces or curly brackets.

Any help on this would be great.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Leon
  • 616
  • 1
  • 6
  • 10
  • 1
    Can you show us what you have tried so far? – bart s Dec 19 '12 at 10:20
  • 1
    Use a regex, the second answer here has the right one: http://stackoverflow.com/questions/413071/regex-to-get-string-between-curly-braces-i-want-whats-between-the-curly-brace – ThomasM Dec 19 '12 at 10:28

5 Answers5

4

Here is how you can do this:

var ps = document.getElementsByTagName('p');
    foreach = Array.prototype.forEach;

foreach.call(ps, function (p) {
    var content = p.innerHTML;
    p.innerHTML = content.replace(/\{(.*?)\}|\((.*?)\)/g, function (m) {
        return '<span style="font-weight: bold;">' + m + '</span>';
    });
});
​

And of course a fiddle. For the example you need just pure JavaScript, no additional libraries.

  • Edit:

If you don't want to see the brackets in the result you can use:

var ps = document.getElementsByTagName('p');
    foreach = Array.prototype.forEach;

foreach.call(ps, function (p) {
    var content = p.innerHTML;
    p.innerHTML = content.replace(/\((.*?)\)|\{(.*?)\}/g, function (m) {
        return '<span style="font-weight: bold;">' + m.replace(/[\(\)\{\}]/g, '') + '</span>';
    });
});

Fiddle: http://jsfiddle.net/ma47D/4/

​ Best regards!

Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
1

You can do it with mootools like this:

window.addEvent('domready', function()
{
    $$('P').each(function(p)
    {
        p.set('html', p.get('text').replace(/{([^\}]*)*}/g,"<b>$1</b>"));
    });
});​

domready is important because it must be done after page is completely loaded. converting to jquery would not be so hard.

http://jsfiddle.net/Smw7Q/1/

Ali
  • 21,572
  • 15
  • 83
  • 95
0

Locally you can handle it like this:

<!DOCTYPE html>
<html>
    <head>
        <script>
            function transfer(){
                document.getElementById("result").innerHTML=document.getElementById("demo").value.replace(/{/g,'<strong>').replace(/}/g,'</strong>');
            }
        </script>
    </head>
    <body>
        Input: <input type="text" name="input" id="demo"><br>
        <input type="button" value="Submit" onclick="transfer();">
        <p id="result"></p>
    </body>
</html>

If you submit the text to server, the magic can be done similarly at server side.

coderLMN
  • 3,076
  • 1
  • 21
  • 26
0

My suggestion

<!DOCTYPE html>
<html>
    <head>
        <style>
        p span {
            font-size:1.5em;
        }
        </style>
        <script>
        function regex(){
            document.getElementById("output").innerHTML=
                document.getElementById("input").value.replace(/{(.*?)}/g, "<span>$1</span>");
        };
        </script>
    </head>
    <body>
        <p id="output"></p>
        <textarea id="input" rows="30" cols="80"></textarea>
        <input type="button" value="Input" onclick="regex();"/>
    </body>
<html>

Of course, prior to submitting, you need to sanitize your data.

mkey
  • 535
  • 2
  • 12
-1

If tried something, but I'm sure there are more elegant solutions.

http://jsfiddle.net/xT7Fg/

$(document).ready(function(){
    $(tb).blur(function(){
        var str  = '';
        var nextFont = 0;
        $.each($(tb).val(),function(i,char){
            if(nextFont == 0){
                if(char == '{'){
                    if($(tb).val().indexOf(i,'}')){
                        str += '<font size="15">';
                        nextFont = $(tb).val().indexOf('}', i);
                    } else {
                        str += char;                    
                    }
                } else {
                   str += char;                
                }
            } else if (nextFont === i) {
                str += '</font>';
                 nextFont = 0;               
            } else {
                str += char; 
            }      
        });
        $("#txt").html(str);
    });
});
mkey
  • 535
  • 2
  • 12
CyclingFreak
  • 1,614
  • 2
  • 21
  • 40
  • The `font` tag is deprecated; better to use `strong` or `span`. The if...else structure could be simplified quite a lot, e.g. http://jsfiddle.net/xT7Fg/1/ – Stuart Dec 19 '12 at 10:59
  • I know. It was quick and dirty. The goal was to show one way of a algorithm. – CyclingFreak Dec 19 '12 at 12:41