I've seen some similar questions to mine here, but they don't really answer me...
So I'm doing this: (Inside the document ready function)
$("#dest").focusin(function() {
$("#dest").val($.trim($("#dest").val()));
});
The ideia is when the user focus on a input called #dest trim all the space characters on it (previously added using focusout for visual comfort).
Right now, nothing is happening. :(
Hope someone can help me a bit here.
Thanks!
Is this a computer related problem?
I've tested all the code provided by commenters and none works. I'm using Firefox and Safari under OSX (Snow Leopard) 10.6.8 and also Safari under 10.8.2 (Lion) and I got the same results... OSX problem? -- Everything is ok, check my last edit!
Final Edit and Solution thanks to Phil Klein
My problem was using incorrectly jQuery's trim() function... According to the trim() documentation it does the following:
The $.trim() function removes all newlines, spaces (including
non-breaking spaces), and tabs from the beginning and end of the
supplied string. If these whitespace characters occur in the middle of
the string, they are preserved.
Yesterday I didn't read the last part where it says from the beginning and end of the supplied string -- Sorry everyone. :(
Lucky and after the drawing above, #Phil Klein understood my mistake and helped me with a solution:
$(function() {
$("#dest").on("focus", function() {
var dest = $(this);
dest.val(dest.val().split(" ").join(""));
});
});
You can read more about the solution and see an example here.
Thanks to #Phil Klein and also everyone who helped me on this one ;)
The example below removes all spaces from the contents of the textbox on focus. This particular example requires jQuery 1.7+ since it uses the new .on() API:
$(function() {
$("#dest").on("focus", function() {
var dest = $(this);
dest.val(dest.val().split(" ").join(""));
});
});
See this example: http://jsfiddle.net/RnZ5Y/5/
Try these : $.trim($("#dest").val());
correct me if i am wrong !!
try $("#dest").val().trim();,it worked for me.
If you're code above is executing before the page isn't fully ready then it's very likely that the #dest isn't found by jquery and the code for listening to the event doesn't get executed.
This function is working fine for your scenario. As it is taking only one space between character and not allow more than 2 space
$(function() {
$("#dest").on("focusout", function() {
var dest = $(this);
dest.val(jQuery.trim(dest.val()));
dest.val(dest.val().replace(/[ ]{2,}/, ' '));
});
});
I made my own function :)
look here please :)
function KillSpaces(phrase) {
var totalPhrase = "";
for (i = 0; i < phrase.length; i++)
{
if (phrase[i] != " ")
{
totalPhrase += phrase[i];
}
}
return totalPhrase;
}
you can easily use it when ever you want to!
$(document).ready(function(){
var test="for testing purposes only";
alert(killSpaces(test));
});
Related
I am trying to use jQuery Based text highlight plugin, it works for single word highlight but breaks when i pass array, My syntax seems to be correct as the the documentation http://bartaz.github.io/sandbox.js/jquery.highlight.html
Example: http://jsfiddle.net/YyAXP/6/
//$('#article').highlight("me");
$("#article").highlight(["me","highlight","plugin"]);
I need to pass several keywords to this function so that it highlight all of them.
Solved:
It seems script had bug which was resolved use the following fiddle with complete script for array based search highlight script source
Fiddle: http://fiddle.jshell.net/ogyyvvog/2/
It gets error when running your code
pat.toUpperCase is not a function
pat should be array, maybe you can fix it in this way?
return this.length && pat && pat.length ? this.each(function () {
for(var i=0;i<pat.length;i++)
innerHighlight(this, pat[i].toUpperCase());
}) : this;
jsfiddle
Declaration syntax is correct
$("#article").highlight(["me","highlight","plugin"]);
You just need to correctly include the plugin in your jsfiddle. Do not include tag script, use instead "External Resources" menu... check updated demo
You can use my highlighting plugin jQuiteLight, that can easily work with both arrays and also regular expressions.
// for strings
$(".element").mark("query here");
// for RegExp
$(".element").mark(new RegExp(/query reg[a-zA-Z]+/));
// for array
$(".element").mark(["string query", new RegExp(/query arr[a-z]+/)]);
I'm trying to remove redundant dashed lines from a client's blog site because its erratic lengths are butting heads with the responsive design. Example: ---------------------------------
I created a jquery plug-in to remove individual lines from one post:
$('p:contains(——————————————————————————————-),
p:contains(———————————————————————–),
p:contains(————————————————————————————-),
p:contains(———————————————————————————),
p:contains(——————————————————————–),
p:contains(————————————————————-),
p:contains(————————————————————————————————),
p:contains(—————————————————————————),
p:contains(————————————————————),
p:contains(———————————————————————————–),
p:contains(——————————————————————————-)').each(function() {
$(this).remove();
});
But even this method is redundant lol. I tried rewriting like so using regex:
$('p:contains(----)').each(function(text) {
var hyphen = text.replace(/\u00AD/g,'');
return hyphen;
});
It didn't work and I've since been stuck on it for an hour. If anyone could give me a push in the right direction I would greatly appreciate it. Thanks!
Giving the jQuery .text() method a function is the way to perform a direct replacement of an element's text. The function receives the old text as its second argument, and whatever it returns will be used as the replacement.
$("p:contains(---)").text(function(i, text) {
return text.replace(/-{3,}/g, '');
});
So Ive been working on this error for a while now, and I cant figure it out for the life of me. There are a couple questions on stackoverflow about this, but they dont pertain to me, at least I dont think so. I am pretty new to the programming world. But this is my situation.
The following code writes text in a textbox with setTimeout.
putftp.onclick = function () {
var Text = document.getElementById("TextBox");
function firsttext() {
Text.innerHTML += "This is the first test.";
setTimeout(secondtest, 3000);
}
function secondtest() {
Text.innerHTML += "<br/>This is the second test.";
//setTimeout(thirdtest, 5000);
}
firsttext();
};
My main goal was to start a new line (with the br), but for some reason, IE throws a fit and says its an unknown runtime error. Note that the code works perfect without the br but I would like for the "This is the second test." To start on a new line. Ive tried alot of things to fix this such as putting a div around the textbox, looking for stray br s in the code, and even getting rid of the textbox and just having a div works. But for some reason a textbox and the br arent working out together. Im using IE to test in and asp.net.
Anyone know the answer to this? Thanks for your help in advance!!
Update
\n works, but does not begin the text at a new line.
gives the same result at
You may want to try <br /> Notice the space after the r and before the slash.
The first argument to setTimeout is a string:
setTimeout('secondtest()', 3000);
Use Text.value instead of Text.innerHTML. IE doesn't like the innerHTML property when trying to insert a new line (and definitely use \n instead of <br />).
I figured it out but thanks to all who tried.
I ended up using \n with the original textbox and changed Text.innerHTML to Text.innerTEXT
Hi can someone convert this jquery in to plain javascript?
$("body *").each(function () {
$(this).html($(this).html().replace(/\[br\]/\g,'<br/>'));
});
What it does is, it finds all [br] and then replace it with <br/>
The code above works perfectly in chrome but not in mozilla and IE so i need to execute it in plain javascript. many thanks to all!
Try this:
window.onload=function(){
document.body.innerHTML = document.body.innerHTML.replace( /\[br\]/g,'<br/>');
}
ps. In your code, there is a bug: instead of /\[br\]/\g should be /\[br\]/g
The problem was that you had an illegal character in your regular expression.
This works: $(this).html($(this).html().replace(/\[br\]/g,'<br/>'));
Live example: http://jsfiddle.net/tqksm/
the problem is not jQuery. You have first to take a reference to the current html content, then apply the replace and finally inject the new html:
$("body *").each(function () {
var $this = $(this);
var html = $this.html();
$this.html(html.replace(/\[br\]/\g,'<br/>'));
});
I think your missing the point here...rewriting it in plain Javascript is likely to only make it worse for you. One of jQuery's purposes is to take away all the pain that comes while writing Javascript that must work on all browser. So...I think you'd be best off if you start looking for an alternative approach on your jQuery code instead of rewriting it to plain Javascript.
i need your help with an regex in Javascript, again.
Sometimes i have empty p-Tags in the DOM and i will remove them all.
Some look like:
<p></p>
others like:
<p> </p>
I think for an regex pro is this very lame, but i'm not good with it :(
so thanks in advance :)
You don't need to use regex for something this simple. Most likely you need to iterate over all the 'p' elements anyway. Something like this works:
http://jsfiddle.net/mqchen/3pV2P/
$('p').each(function(index, item) {
if($.trim($(item).text()) === "") {
$(item).slideUp(); // $(item).remove();
}
});
A very basic regex for this:
<p>\s*<\/p>
And here's a simple RegEx Tester for Firefox that might help you in the future. I use it quite a bit.
that's some methods that helps sometimes
$('p:empty').hide()
p:empty { display: none; }
The accepted answer is of course completely correct but since the poster asked for how to use regular expressions, here is a regex version (for completeness if you will) that does just that:
$('p').map(function() {
if( /^[\s ]*$/.test($(this).text()) ) {
$(this).remove();
}
});
Note a better regex version can be achieved by using the jQuery regex selector extension