Solved

Highlight a word from some text

Posted on 2016-08-01
16
49 Views
Last Modified: 2016-08-01
I have a page that displays text from a nvarchar (8000) field in my database as: <%=(report_results.Fields.Item("comments").Value)%>

I am also passing a 'keyword' from the previous page and I retrieve it with: request.form("keyword")  

I want to highlight the value I pass as keyword in the text that is displayed on the page.  Example:

I my keyword is "second"

I want the text to display as "This is the second time I try this"  or something of that nature. How can I do this ?

I am using SQL 2008 and ASP Classic
0
Comment
Question by:amucinobluedot
  • 8
  • 8
16 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41738324
what you see in your page is generated and displayed as HTML. Hence, to highlight certain words based on certain actions, you need to change the HTML codes as well.

so, now the question is what is the container of that area to display your wordings? is it a DIV, etc? do you have the codes to post here?
0
 

Author Comment

by:amucinobluedot
ID: 41738332
Sure. This is the whole table where I have a repeated region showing the results.

 <table class="table table-striped table-bordered table-hover dataTables-example" >
                <thead>
                  <tr>
                    <th width="3%">Actions</th>
                    <th width="10%">User</th>
                    <th width="46%">Notes</th>
                    <th width="10%">Case number</th>
                    <th width="13%">Contact</th>
                    <th width="8%">Last updated</th>
                    <th width="10%">Case status</th>
                    </tr>
                </thead>
                <tbody>
                <% 
While ((Repeat1__numRows <> 0) AND (NOT report_results.EOF)) 
%>
                    <tr class="gradeX">
                      <td><a href="../cases/notes_index.asp?caseId=<%=(report_results.Fields.Item("caseid").Value)%>" target="_blank" class="btn btn-white btn-sm"><i class="fa fa-folder" text-navy></i></a></td>
                      <td><%=(report_results.Fields.Item("atty").Value)%></td>
                      <td><%=(report_results.Fields.Item("comments").Value)%></td>
                      <td><%=(report_results.Fields.Item("caseid").Value)%></td>
                      <td><%=(report_results.Fields.Item("Alien").Value)%></td>
                      <td><%= DoDateTime((report_results.Fields.Item("Lastupdate").Value), 2, 9225) %></td>
                      <td><%=(report_results.Fields.Item("casestatus").Value)%></td>
                    </tr>
                                    <% 
  Repeat1__index=Repeat1__index+1
  Repeat1__numRows=Repeat1__numRows-1
  report_results.MoveNext()
Wend
%>
                </tbody>
                <tfoot>
                  <tr>
               <th width="3%">Actions</th>
                    <th width="10%">User</th>
                    <th width="46%">Notes</th>
                    <th width="10%">Case number</th>
                    <th width="13%">Contact</th>
                    <th width="8%">Last updated</th>
                    <th width="10%">Case status</th>
                    </tr>
                    

                </tfoot>
              </table>

Open in new window


The field that will include the text that should highlight any matching keywords is: <%=(report_results.Fields.Item("comments").Value)%>

Let me know if you need anything else.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41738335
is that the content of report_results.Fields.Item("comments").Value is plain text or already in HTML?
0
 

Author Comment

by:amucinobluedot
ID: 41738337
It is HTML code. User saves the content from an HTML editor, this is an example:

<p><strong>Client is traveling to:</strong> <span style="color:#ff0000">This is an example of a note</span></p><p>From:<br />To:</p><p>Additional comments:&nbsp;</p>

Open in new window


This is another example where user doesn't do anything other than enter text in the editor

test note no styling

Open in new window

0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41738367
you probably can refer to this article:

Keyword highlighting in ASP
http://www.codeproject.com/Articles/610/Keyword-highlighting-in-ASP

then try something like:
<%=Highlight(report_results.Fields.Item("comments").Value, "second"  , "<font color=red><b>", "</b></font>"  )%>

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41738400
I made a mistake and closed the question. Unfortunately it didn't work, it doesnt highlight the word  :$
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41738402
let me try and get back to you shortly..
0
 

Author Comment

by:amucinobluedot
ID: 41738404
Sounds good. Same result with other browsers
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41738407
simple test looks good:
<%


text = "<p><strong>Client is traveling to:</strong> <span style=""color:#ff0000"">This is an example of a note</span></p><p>From:<br />To:</p><p>Additional comments:&nbsp; whatever this is second ++ </p>"

response.write Highlight(text, "second"  , "<font color=red><b>", "</b></font>"  )

Function Highlight(strText, strFind, strBefore, strAfter)
	Dim nPos
	Dim nLen
	Dim nLenAll
	
	nLen = Len(strFind)
	nLenAll = nLen + Len(strBefore) + Len(strAfter) + 1

	Highlight = strText

	If nLen > 0 And Len(Highlight) > 0 Then
		nPos = InStr(1, Highlight, strFind, 1)
		Do While nPos > 0
			Highlight = Left(Highlight, nPos - 1) & _
				strBefore & Mid(Highlight, nPos, nLen) & strAfter & _
				Mid(Highlight, nPos + nLen)

			nPos = InStr(nPos + nLenAll, Highlight, strFind, 1)
		Loop
	End If
End Function

%>

Open in new window


>>Ok. For some reason it only highlited in red the first time I did it, then the results page keeps highlighting that one word.
Do you have a screenshot to share here?
0
 

Author Comment

by:amucinobluedot
ID: 41738416
Well I think the problem is that I copy/pasted the code. It is always using 'second' to highlight, I should change it to the value of the form

<%=Highlight(report_results.Fields.Item("comments").Value, "second"  , "<font color=red><b>", "</b></font>"  )%>

Open in new window


How can I replace 'second'  for <%=request.form("keyword")%>   ?
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41738419
alright, I think you got the problem resolved.
0
 

Author Comment

by:amucinobluedot
ID: 41738421
Not 100% ... just need a little help with the syntax to use the form value instead of the hard coded value of 'second'
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41738429
alright, so try this:
<%=Highlight(report_results.Fields.Item("comments").Value, request.form("keyword")  , "<font color=red><b>", "</b></font>"  )%>

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41738433
Awesome !  thanks so much. all set here.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41738434
coool, glad that it works!
0
 

Author Comment

by:amucinobluedot
ID: 41738435
It does, perfectly. Have a good night!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This video teaches users how to migrate an existing Wordpress website to a new domain.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now