[jQuery] How to make the first word bold.
I am trying to make the first word of a sentence (a title from a generated feed) bold. TURN THIS: div id=title a href=123.htmlOne Two Three/a /div INTO THIS: div id=title a href=123.htmlstrongOne/strong Two Three/a /div I sure there is a way to do with jQuery, but I can't quite figure it out. I have tried something like this, with no luck: $(#title.a).contents(0).wrap(strong/strong); Any help would be greatly appreciated.
[jQuery] Re: How to make the first word bold.
Thanks Wizzud... that's what I was looking for. I had to wrap window.onload = (function(){try{ ... }catch(e){}}); around the script... not sure why, but it seems to work On Nov 12, 5:48 pm, Wizzud [EMAIL PROTECTED] wrote: eg. $('#links a').each(function(){ var me = $(this); me.html( me.text().replace(/(^\w+)/,'strong$1/strong') ); }); or $('#links a').each(function(){ var me = $(this) , t = me.text().split(' '); me.html( 'strong'+t.shift()+'/strong '+t.join(' ') ); }); etc On Nov 12, 9:17 pm, Andy Matthews [EMAIL PROTECTED] wrote: Viktor... You'd probably need an $.each() on there then. -Original Message- From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Viktor Tarm Sent: Monday, November 12, 2007 2:35 PM To: jQuery (English) Subject: [jQuery] Re: How to make the first word bold. Thank you all for your help. Andy, I'm looking to bold the first WORD, of all the links, not the first link. Karl, I made a simple page to test your code. It looks like this: script type=text/javascript src=./src/jquery-1.2.1.pack.js/ script script type=text/javascript $(document).ready(function() { var mystring = $('#links a').html(); var firstword = mystring.replace(/(^\w+)/,'strong$1/strong'); $('#links a').html(firstword); }); /script /head body div id=links p class=titlea href=a.htmlone two three four five/a/p p class=titlea href=b.htmlsix seven eight/a/p p class=titlea href=c.htmlnine ten eleven/a/p p class=titlea href=d.htmltwelve thirteen fourteen/a/p /div This results in changing the content to = the first link's content, like so: div id=links p class=titlea href=a.htmlstrongOne/strong two three four five/a/p p class=titlea href=b.htmlstrongOne/strong two three four five/a/p p class=titlea href=c.htmlstrongOne/strong two three four five/a/p p class=titlea href=d.htmlstrongOne/strong two three four five/a/p /div I'm looking to just make the first word of the link bold (without changing the content), like this: div id=links p class=titlea href=a.htmlstrongOne/strong two three four five/a/p p class=titlea href=b.htmlstrongsix/strong seven eight/ a/p p class=titlea href=c.htmlstrongnine/strong ten elevan/ a/p p class=titlea href=d.htmlstrongtwelve/strong thirteen fourteen/a/p /div Any ideas? Thanks again. On Nov 12, 12:24 pm, Karl Swedberg [EMAIL PROTECTED] wrote: You could also do this with a regular expression. Maybe something like this: var mystring = $('#title a:first').html(); var firstword = mystring.replace(/(^\w+)/,'strong$1/strong'); $('#title a:first').html(firstword); --Karl _ Karl Swedbergwww.englishrules.comwww.learningjquery.com On Nov 12, 2007, at 11:52 AM, [EMAIL PROTECTED] wrote: Somebody else probably has a more consice method but you could try something like this var mystring = One Two Three; //replace with your string var stringarray = mystring.split( ); var firstword = mystring.split( )[0]; myarray = jQuery.grep(stringarray, function(n, i){ return (i 0); }); $(div#title a).append(strong + firstword + /strong ).append(myarray.join( )); On Nov 11, 8:56 pm, Viktor Tarm [EMAIL PROTECTED] wrote: I am trying to make the first word of a sentence (a title from a generated feed) bold. TURN THIS: div id=title a href=123.htmlOne Two Three/a /div INTO THIS: div id=title a href=123.htmlstrongOne/strong Two Three/a /div I sure there is a way to do with jQuery, but I can't quite figure it out. I have tried something like this, with no luck: $(#title.a).contents(0).wrap(strong/strong); Any help would be greatly appreciated.
[jQuery] Re: How to make the first word bold.
Thanks a lot Wizzud... works perfectly. On Nov 12, 5:48 pm, Wizzud [EMAIL PROTECTED] wrote: eg. $('#links a').each(function(){ var me = $(this); me.html( me.text().replace(/(^\w+)/,'strong$1/strong') ); }); or $('#links a').each(function(){ var me = $(this) , t = me.text().split(' '); me.html( 'strong'+t.shift()+'/strong '+t.join(' ') ); }); etc On Nov 12, 9:17 pm, Andy Matthews [EMAIL PROTECTED] wrote: Viktor... You'd probably need an $.each() on there then. -Original Message- From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Viktor Tarm Sent: Monday, November 12, 2007 2:35 PM To: jQuery (English) Subject: [jQuery] Re: How to make the first word bold. Thank you all for your help. Andy, I'm looking to bold the first WORD, of all the links, not the first link. Karl, I made a simple page to test your code. It looks like this: script type=text/javascript src=./src/jquery-1.2.1.pack.js/ script script type=text/javascript $(document).ready(function() { var mystring = $('#links a').html(); var firstword = mystring.replace(/(^\w+)/,'strong$1/strong'); $('#links a').html(firstword); }); /script /head body div id=links p class=titlea href=a.htmlone two three four five/a/p p class=titlea href=b.htmlsix seven eight/a/p p class=titlea href=c.htmlnine ten eleven/a/p p class=titlea href=d.htmltwelve thirteen fourteen/a/p /div This results in changing the content to = the first link's content, like so: div id=links p class=titlea href=a.htmlstrongOne/strong two three four five/a/p p class=titlea href=b.htmlstrongOne/strong two three four five/a/p p class=titlea href=c.htmlstrongOne/strong two three four five/a/p p class=titlea href=d.htmlstrongOne/strong two three four five/a/p /div I'm looking to just make the first word of the link bold (without changing the content), like this: div id=links p class=titlea href=a.htmlstrongOne/strong two three four five/a/p p class=titlea href=b.htmlstrongsix/strong seven eight/ a/p p class=titlea href=c.htmlstrongnine/strong ten elevan/ a/p p class=titlea href=d.htmlstrongtwelve/strong thirteen fourteen/a/p /div Any ideas? Thanks again. On Nov 12, 12:24 pm, Karl Swedberg [EMAIL PROTECTED] wrote: You could also do this with a regular expression. Maybe something like this: var mystring = $('#title a:first').html(); var firstword = mystring.replace(/(^\w+)/,'strong$1/strong'); $('#title a:first').html(firstword); --Karl _ Karl Swedbergwww.englishrules.comwww.learningjquery.com On Nov 12, 2007, at 11:52 AM, [EMAIL PROTECTED] wrote: Somebody else probably has a more consice method but you could try something like this var mystring = One Two Three; //replace with your string var stringarray = mystring.split( ); var firstword = mystring.split( )[0]; myarray = jQuery.grep(stringarray, function(n, i){ return (i 0); }); $(div#title a).append(strong + firstword + /strong ).append(myarray.join( )); On Nov 11, 8:56 pm, Viktor Tarm [EMAIL PROTECTED] wrote: I am trying to make the first word of a sentence (a title from a generated feed) bold. TURN THIS: div id=title a href=123.htmlOne Two Three/a /div INTO THIS: div id=title a href=123.htmlstrongOne/strong Two Three/a /div I sure there is a way to do with jQuery, but I can't quite figure it out. I have tried something like this, with no luck: $(#title.a).contents(0).wrap(strong/strong); Any help would be greatly appreciated.