Reputation: 39
I am attempting to create a JavaScript bookmarklet that will simply highlight a specific word. This word will be static and I do not want input from the user. As an example, I would want this to highlight the word "test" on the current page. I only need the first instance.
Additional and likely unnecessary information: This will actually be looking at the source code of the page, which I have set up and working. I am using the following to open the source in the current tab:
javascript:window.location = "view-source:" + window.location.href;
I am hoping to amend this with a script that will then highlight the instance of the word "test". I appreciate any help. Searches have shown how to prompt a user for a term and then run it to Google, but not to simply highlight it on the current page.
Upvotes: 3
Views: 429
Reputation: 672
Maybe this thing can be done with String.replace method. You can replace occurrences of the needed word with this word wrapped in a span tag with some class (<span class="highlight">word</span>
).
Also, such methods as String.indexOf, String.substr and HTMLElementObject.innerHTML might be useful as well.
UPD: It seams that there are a bunch of ready solutions in the Internet. For example, Google with this query: "js highlight word -jquery" lead to these pages:
Upvotes: 1