Click here to Skip to main content
Rate this: bad
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
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
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
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();
		$('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;
					$(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

            } catch (ex) { e.preventDefault(); }

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

  Print Answers RSS
0 OriginalGriff 476
1 Sergey Alexandrovich Kryukov 235
2 CHill60 195
3 Afzaal Ahmad Zeeshan 145
4 TheRealSteveJudge 120
0 Sergey Alexandrovich Kryukov 9,121
1 OriginalGriff 7,920
2 Peter Leow 4,774
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,176

Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2015
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