Highlighting search words like Chrome with jQuery - javascript

I have recently done a very simple highlighting with jQuery and a highlight plugin. It looks like this:
$('myButton').click(function() {
$('body').highlight($('#myInputText').val());
});
But I wonder how can I do the Chrome like highlighting, I mean highlight the letters whenever I type in some letter in textbox without submitting. I think maybe use a keyup event... Any ideas?
Thanks Andy, i changed 'this[0]' to 'search[i]' in your code and it works if there is only one 'p' tag
$(document).ready(function(){
var search = ['p', 'div', 'span'];
$("#highlighter").bind('keyup', function(e){
var pattern = $(this).val();
$.each(search, function(i){
var str = search[i];
var orgText = $(str).text();
orgText = orgText.replace(pattern, function($1){
return "<span style='background-color: red;'>" + $1 + "</span>"
});
$(str).html(orgText);
});
});
});

I made quick excersise out of it, code:
$(document).ready(function(){
var search = ['p', 'div', 'span'];
$("#highlighter").bind('keyup', function(e){
var pattern = $(this).val();
$.each(search, function(i){
var str = this[0];
var orgText = $(str).text();
orgText = orgText.replace(pattern, function($1){
return "<span style='background-color: red;'>" + $1 + "</span>"
});
$(str).html(orgText);
});
});
});​​
link: http://jsbin.com/amica3/edit

$('#myInputText').keypress(function(e) {
switch (e.keyCode) {
case 13: // "Enter" was pressed; handle it if you want
return false;
case 27: // ESC was pressed; handle it if you want
return false;
}
$('body').highlight($(this).val());
});

I've edited JAndy's code so the result would match all the occurences in the text and the search would not be case-sensitive.
Link
Hope someone finds this helpful

Related

search case insensitive letters in textbox

I am trying to search the contents using jquery. Search option working fine. But, I have faced some case insensitive problem here. My full codes on jsfiddle. if i put senthil in my search it didn't show the result. Because, I have Senthil (Uppercase S) in my content. How do I find Uppercase letters in jquery?
JsFiddle
$('#search').on('input', function(){
var text = $(this).val();
$('.subjects a').show();
$('.subjects a:not(:contains(' + text + '))').hide();
$('.subjects a span').show();
});
You can use a custom expression for contains like this:
$.expr[":"].contains = $.expr.createPseudo(function(arg) {
return function( elem ) {
return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
};
});
Updated Fiddle
Add this code to your JS code
$.expr[":"].contains = $.expr.createPseudo(function(arg) {
return function( elem ) {
return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
};
});
Credits Here

When click a button add value to textarea

<script type="text/javascript">
$(document).ready(function() {
$(".buttons").click(function(){
var cntrl = $(this).html();
$("#txt-area").text(cntrl+",");
//alert (cntrl);
});
});
</script>
When I click to my character on my html page character's value sets to textarea, but when I click to another character previous char disappears. I know about something about arrays in JS but how can I handle this.
How can I add values to textarea properly without disappearing?
Try
$(document).ready(function () {
$(".buttons").click(function () {
var cntrl = $(this).html();
$("#txt-area").val(function (_, val) {
return val + cntrl + ","
});
//alert (cntrl);
});
});
Demo: Fiddle
or
$("#txt-area").text($("#txt-area").val() + cntrl+",");
$("#txt-area").append(cntrl + ",");
Demo: Fiddle - I prefer using val() because I consider it as a input element
Arun's answer above works, but it will add a comma to the end. If you want to avoid that, place the comma before the new value, and then have the code check to see if this is the first time something is being added, and not place a comma that time.
$(document).ready(function () {
var oldvalue;
var newvalue;
$(".buttons").click(function () {
oldvalue = $("#txt-area").val(); //GET THE VALUE OF TEXTBOX WHEN USER CLICKS
if (oldvalue) {newvalue = ', '+$(this).html();} // IF THE TEXTBOX ISN'T BLANK, PLACE A COMMA BEFORE THE NEW VALUE
else {newvalue = $(this).html();} //IF THE TEXTBOX IS BLANK, DON'T ADD A COMMA
$("#txt-area").val(oldvalue + newvalue); //PLACE THE ORIGINAL AND NEW VALUES INTO THE TEXTBOX.
});
});
Try,
$(document).ready(function() {
$(".buttons").click(function(){
var cntrl = $(this).html();
var xTxtArea = $("#txt-area");
xTxtArea.text(xTxtArea.text() + cntrl + ",");
});
});

Regex jQuery find("option:[text^='"]) error above ie 8 and chrome

I have a jScript function to text search an element in drop down. It used to work fine till ie7. I have a workaround that works cross browsers but is slow in ie7 using jQuery option:contains instead of Regex.
Function:
/// For the dropdown element passed, find the index where the Text matches the passes string
/// and select this option. Returns true if found otherwise false
function selectTextinDropdown(el, sometext) {
// Use REgex instead of option:contains as it it much faster!
$(el).find("option:[text^='" + sometext.trim() + "']").each(function () {
// works ok but SLOW in IE 7!!
// $(el).find("option:contains('" + sometext.trim() + "')").each(function () {
//alert("found|" + this.text + "|" + sometext);
$(this).attr("selected", "selected");
if ($(this).text() == sometext) {
$(this).attr("selected", "selected");
return true; //found and selected!
}
return false; //Not found and Not selected!
});
}
Anybody familiar with a better workaround?
tks for reading!
Try this:
function selectTextinDropdown(selectEle, targetText) {
if(targetText==null){
return false;
}
targetText = targetText.trim();
// Find all options that meet your condition
var $matches = $(selectEle).find('option').filter(function(index){
// only use jquery selector once
var $this = $(this);
// extract text from the option
var text= $this.text();
// determine if it's found
// OPTION A: exact match
var found = (text === targetText);
// OPTION B: partial match
// var found = (text.indexOf(targetText) >= 0);
if(found){ // select item if found
$this.attr("selected", "selected");
}
return found;
});
return ($matches!=null && $matches.length>0);
}

Show element if has same html and if not make an alert saying "no results"

Sorry for the horrible title, Am terrible at wording these things.
What I am trying to do is quite simple I think.
I have a set of hidden letters that make up a word.
Below them is a selection of random jumbled up letters.
When people click one of the random jumbled letters I am filtering through the hidden letters and showing the corresponding letter.
What I need to do is, if someone clicks a letter, filter through the hidden letters and either return a "true" and show the letter or return a "false/null" and make an alert();
This is how I am filtering at the moment. I am confused as to where to place an if statement or if that is even the approach I should be taking.
And here is a fiddle (the hidden word is "Seal") - http://jsfiddle.net/GA7WB/
var $buttons = $('#letters span'),
$hidden = $('.letter');
$buttons.click(function(){
_selected = $(this).html();
$hidden.filter(function() {
return $(this).text() == _selected;
}).show();
});
You just need to check the length of the results returned by the filter:
// get matched elements
var matches = $hidden.filter(function() {
return $(this).text() == _selected;
});
// show them, or alert if none
if (matches.length > 0) matches.show();
else alert("There are no " + _selected + "'s");
See Fiddle
Try setting a flag if you find one:
var $buttons = $('#letters span'),
var $hidden = $('.letter');
$buttons.click(function(){
_selected = $(this).html();
var foundOne = false;
$hidden.filter(function() {
var retval = $(this).text() == _selected;
if (retval) foundOne = true;
return retval;
}).show();
if (!foundOne) {
alert("Nope");
}
});
FIDDLE http://jsfiddle.net/GA7WB/4/

jQuery replace all single character

I want to replace all characters in the textarea by a click using jQuery.
For example:
ə = e, ı = i, ...
Thıs ıs əxamplə
By clicking it should be:
This is example
$('textarea').html($('textarea').html().replace(/ə/g,'e'))
Adding on from Zikes
var replace_map={
"ı":"i",
"ə":"e"
};
$('textarea').click(function(){
var ret='';
$.each(this.value.split(''), function(i, str) {
ret += replace_map[str] || str;
})
this.value = ret;
});
DEMO
UPDATED EDIT
var replace_map={
"ı":"i",
"ə":"e"
};
$('textarea').click(function(){
this.value = $.map(this.value.split(''), function(str) {
return replace_map[str] || str;
}).join('');
});
UPDATED DEMO
HTML:
<textarea>Thıs ıs əxamplə</textarea>
JS:
var replace_map={
"ı":"i",
"ə":"e"
};
$('textarea').click(function(){
this.value = this.value.replace(/./g,function(str){
return replace_map[str] || str;
})
});
I don't think you really need jQuery for that other than perhaps to select the textarea element (and then only for a microscopic amount of ease).
Past that you should be able to use just string.replace on the textarea content:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

Categories

Resources