Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript HTML DOM
I am trying to highlight (temporarily) words in html. I can successfully edit the text content so that the
 <mark> 
tags are in the correct place in the text string, and it appears I am saving the edited string correctly, BUT....
 
the text displayed in the browser does not change!
Posted 22-Sep-12 17:16pm
Comments
Wes Aday at 22-Sep-12 22:47pm
   
And how do you expect us to know how you screwed things up when you do not show us what you have done?
Member 9444740 at 24-Sep-12 15:13pm
   
am showing the code now...can you help?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

here is the code:
  $(document).ready(function (e) {
        $('#submit').click(function (e) {
            try {
                var $inputTxt = $('#search');
                result = $inputTxt.val();
                alert(result);
	
 
		$('p').each(function () { 
 
			var a=$(this).html().toString();			
			var aindx=[];
 
			while(a.indexOf(result) >= 0){
 
				//alert("string found "+a.indexOf(result)+" position");

				var b=a.substring((a.indexOf(result)+result.length));
 
				if(aindx.length > 0) {
					var prev=aindx.length-1;
					aindx[aindx.length]=a.indexOf(result)+aindx[prev];
					$(this).html(a.substring(0,(aindx[aindx.length]-1))+'<mark id="tmprry">'+result+'</mark>'+a.substring(aindx[aindx.length+result.length-1]));
				} else {
			 		aindx[0] =a.indexOf(result);
					$(this).html(a.substring(0,(aindx[0]-1))+'<mark id="tmprry">'+result+'</mark>'+a.substring(aindx[0]));
				}
 
				//insert mark around position

				a=b;
			}
			
                  
                    });
		   
                
              
            } catch (ex) { e.preventDefault(); }
        });
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100