Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
How to change the color of the hightlighted words?
Old 03-24-2010, 11:56 AM How to change the color of the hightlighted words?
Novice Talker

Posts: 10
Trades: 0
Hi, I have a web page which displays a article. Some of the words in the article are highlighted in different colors depending on
how important they are.

Now, I need to select some words, and click a button on the page, to change the color of those words into different color.

Can anyone give me some idea how to approach this? I googled, but didn't find any information on this problem.

Thanks!!!!
hpdp is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-24-2010, 01:19 PM Re: How to change the color of the hightlighted words?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
and do these colours need to change for ALL viewers or just the local client
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 03-27-2010 at 01:22 PM..
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-27-2010, 12:59 PM Re: How to change the color of the hightlighted words?
Novice Talker

Posts: 9
Trades: 0
You can use jQuery to do this:

http://api.jquery.com/addClass/

HTML:
HTML Code:
<p>This are <span class="highlight">special</span> words that are  <span class="highlight">highlighted</span></p>

<p><input type="submit" value="Change color" class="change" /></p>
jQuery:
HTML Code:
$(".change").click(function () {
      $(".highlight").addClass("newcolor");
      return false;
});
CSS:
HTML Code:
.highlight{background:blue}
.newcolor{background:yellow}
paul.h is offline
Reply With Quote
View Public Profile
 
Old 04-06-2010, 03:04 PM Re: How to change the color of the hightlighted words?
Novice Talker

Posts: 11
Name: Robby
Location: Indianapolis
Trades: 0
Pauls reply should fix it. Or you should be able to find the code thats already doing it and use it again and change the colors. might have to create another CSS style
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
indyonline is offline
Reply With Quote
View Public Profile Visit indyonline's homepage!
 
Old 04-08-2010, 03:01 PM problems in using selection.createRange()
Novice Talker

Posts: 10
Trades: 0
Hi, I have a page. When an user select a word and click a button. The color of the selected word will change to different color. But the problem is I only want to change the color of the selected word instead of all the same words through the whole page.
My example code is:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>highlight</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script language="javascript" type="text/javascript">
function findText(snippet) {

var userSelectedRange = document.selection.createRange();
var selectedText = userSelectedRange.text
var selectedHtmlText = userSelectedRange.htmlText;
var replaceText = '<span class="'+snippet+'">'+selectedText+'</span>';

var newSourceCode, count=0;
newSourceCode= document.body.innerHTML;
re = new RegExp(selectedHtmlText, "gi");
newSourceCode= newSourceCode.replace(re,replaceText);
document.body.innerHTML = newSourceCode;
}
</script>

<style type="text/css">
.highlight {
background:#ff0;
color#00f;
}
.blue {
color:blue;
}
.red {
color:red;
}
.orange {
color: orange;
}
.purple {
color: purple;
}
</style>

</head>
<body>
<form action="" method="" name="form1">
<div>
        <button type="button" id="red" value="red" onclick="findText(document.getElementById('red').value)">red</button>&nbsp;&nbsp;
        <button type="button" id="blue" value="blue" onclick="findText(document.getElementById('blue').value)">blue</button>

</div>
</form>
<br>A <span class="orange">strain</span> of <span class="red">Stachybotrys</span> <span class="orange">chartarum</span> was <span class="blue">recently</span> <span class="or
ange">isolated</span> from the <span class="orange">lung</span> of a <span class="purple">patient</span> in <span class="orange">Texas</span> .
<br>A <span class="orange">strain</span> of <span class="red">Stachybotrys</span> <span class="orange">chartarum</span> was <span class="blue">recently</span> <span class="or
ange">isolated</span> from the <span class="orange">lung</span> of a <span class="purple">patient</span> in <span class="orange">Texas</span> .
</body></html>
When I select 'strain' of the first sentence, then click the button 'red'. I only want the color of that word changes. But 'strain' from the 2nd sentence also changes color. (the code only works at IE browser)
How do I solve this problem? I appreciate any suggestions!
Thanks!

Last edited by hpdp; 04-08-2010 at 03:03 PM.. Reason: want to get instant email notification
hpdp is offline
Reply With Quote
View Public Profile
 
Old 04-09-2010, 01:21 PM How to save the changed page after using selection.createRange
Novice Talker

Posts: 10
Trades: 0
Hi, I have a web page. I can select a word and change the color of that word. But
the problem is when I view the source of that page from the browser, I still
see the original html source code instead of the changed one.
My example code is: (only works at IE browser)
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>highlight</title>
<meta  http-equiv="Content-Type" content="text/html; charset=utf-8">

<script  language="javascript" type="text/javascript">
function  findText(snippet) {

var userSelectedRange =  document.selection.createRange();
var selectedText =  userSelectedRange.text
var selectedHtmlText =  userSelectedRange.htmlText;
var replaceText = '<span  class="'+snippet+'">'+selectedText+'</span>';

alert('text='+selectedText);
alert('html='+selectedHtmlText);

var  newSourceCode, count=0;
newSourceCode= document.body.innerHTML;
re  = new RegExp(selectedHtmlText, "gi");
newSourceCode=  newSourceCode.replace(re,replaceText);
document.body.innerHTML =  newSourceCode;
}
</script>

<style  type="text/css">
.highlight {
background:#ff0;
color#00f;
}
.blue  {
color:blue;
}
.red {
color:red;
}
.orange {
color:  orange;
}
.purple {
color: purple;
}
</style>

</head>
<body>
<form  action="" method="" name="form1">
<div>
        <button  type="button" id="red" value="red"  onclick="findText(document.getElementById('red').value)">red</button>&nbsp;&nbsp;
        <button  type="button" id="blue" value="blue"  onclick="findText(document.getElementById('blue').value)">blue</button>

</div>
</form>
<br>A  <span class="orange" id="1">strain</span> of <span  class="red">Stachybotrys</span> <span  class="orange">chartarum</span> was <span  class="blue">recently</span> <span class="orange">isolated</span>  from the <span class="orange">lung</span> of a <span  class="purple">patient</span> in <span  class="orange">Texas</span> .
<br>A <span  class="orange" id="1">strain</span> of <span  class="red">Stachybotrys</span> <span  class="orange">chartarum</span> was <span  class="blue">recently</span> <span class="orange">isolated</span>  from the <span class="orange">lung</span> of a <span  class="purple">patient</span> in <span  class="orange">Texas</span> .
</body>
</html>
Thanks for any help on this problem!!!!
hpdp is offline
Reply With Quote
View Public Profile
 
Old 04-09-2010, 01:22 PM Re: problems in using selection.createRange()
Novice Talker

Posts: 10
Trades: 0
Thanks for reading this post. I solved this problem by assigning each word with an
unique number.
hpdp is offline
Reply With Quote
View Public Profile
 
Old 04-09-2010, 01:26 PM Re: How to change the color of the hightlighted words?
Novice Talker

Posts: 10
Trades: 0
Thanks for your help. I made the site working by using selection.CreateRange().
But I have a problem with the code. Please see my new post at

http://www.webmaster-talk.com/javascript-forum/201745-how-save-changed-page-after-using.html

Thanks!!
hpdp is offline
Reply With Quote
View Public Profile
 
Old 04-09-2010, 02:03 PM Re: How to save the changed page after using selection.createRange
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Javascript works on the document model in memory it does not and cannot change the original source code.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-09-2010, 02:04 PM Re: How to change the color of the hightlighted words?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
merged all the related threads.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to How to change the color of the hightlighted words?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.28318 seconds with 12 queries