Replace function not working with javascript - javascript

How can I strip the $ and , characters from the following value, the code i am using is not working
var asset_value = second_col.find("input[type=text]").val().replace('$', '');
second_col.find("input[type=text]").val() looks like
$1,080.00
Update:
I am not sure why i am getting voted down! the duplicate solution does not solve my question, nor does any answer below, except for this, very strange!
second_col.find("input[type=text]").val(function(i, val) {
return val.replace(/\$|,/g, '');
});
var asset_value = second_col.find("input[type=text]").val();

You can use regex for this:
second_col.find("input[type=text]").val().replace(/[\$,]/g,'').
Assign this value to variable and use it.

Related

Escaping javascript entities in js?

I want to escape javascript entities on client side. For example :-
If my input string is tes"t result should be tes\"t
Is there any inbuilt function provided by jquery for this ?
This is a really crazy, almost stupid shot in the dark on my part, but...
If you're using a server-side language like PHP to output variables' contents into JavaScript, you should use json_encode as this handles ALL escaping for you, regardless of the type of variable.
On the other hand, if you're (I really hope you're not) doing something like this:
var input = "test"t";
And trying to escape that properly while in JavaScript... that's not going to work. It's a syntax error. You need to escape your literals manually.
Kevin van Zonneveld provide a JavaScript equivalent of PHP’s addslashes here :
http://phpjs.org/functions/addslashes/
function addslashes(str) {
// example 1: addslashes("kevin's birthday");
// returns 1: "kevin\\'s birthday"
return (str + '')
.replace(/[\\"']/g, '\\$&')
.replace(/\u0000/g, '\\0');
}
Based on this function, I guess you might want to add this function to prototype of the String like this.
if (!String.prototype.addslashes) {
String.prototype.addslashes = function () {
return this.replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
};
}
var str = 'tes"t';
alert(str.addslashes()); // shows 'tes\"t'
DEMO: http://jsfiddle.net/naokiota/6F6aN/6/
Hope this helps.

Creating a for each loop on multitple letters using jquery

im working on an mobile web application, which uses data from a .jsp page in the same directory. I made it possible to get formdata and put it in a variable using jQuery's .val().
now i have created an array of the input, and want to loop it using the $.each.
Only problem is when i try to create the loop, it separates every letter, instead of words..
This is the code i wrote:
var entrylist= $('#entrylistForm').val ();
/* gettin the formvalue. This looks like this:
[{s.ENTRYID=1480565, s.SHEETID=131444777, s.ENTRYDATE=2012-14-04}]
*/
$.each (entrylist, function (key, value) {
alert( key + "=>" + value+"");
// ...
}
I'm tying to get the array like this:
[0]ENTRYID=1480565,
[1]SHEETID=131444777, etc...
help anyone, i cant figure out what i'm doing wrong..
thnx in advance!
Why don't you use the native .split function, and split by whitespace? Assuming the value is a string.
entrylist = entrylist.split(', ');
You should be able to use the split function.
var entrylist = $('#entrylistForm').val();
var list = str.split(', ');
Which would split it after ", " and put it into an array which you can access.
See: http://www.w3schools.com/jsref/jsref_split.asp

Quotes in Javascript

I have been trying for hours to fix this code, I can't see what's wrong:
document.getElementById('detail'+num).innerHTML='<a class="dobpicker" href="javascript:NewCal('+s_d+','+ddmmyy+')">'
The problem is in href="javascript ..."
s_d is a javascript variable defined as
var num = 2;
var s_d = "sname"+num;
var ddmmyy = "ddmmyy";
Basically I need to call a javascript function with different parameter each time.
Use a backslash like \'.
document.getElementById('detail'+num).innerHTML=
'<a class="dobpicker" href="javascript:NewCal(\''+s_d+'\',\''+ddmmyy+'\')">'
Since this is the value of a href attribute, HTML encode them:
document.getElementById('detail'+num).innerHTML='<a class="dobpicker" href="javascript:NewCal("'+s_d+'","'+ddmmyy+'")">'
Or better yet don't use the javascript: protocol:
[0,1,2,3,4,5].forEach(function(num) {
var s_r = "sname"+num;
var ddmmyy = "ddmmyy";
var aEl = document.createElement("a");
aEl.className = "dobpicker";
aEl.onclick = function() {
NewCal(s_d, ddmmyy);
}
document.getElementById('detail'+num).appendChild(aEl);
});
Your .innerHTML setting is using s_d, but your variable declaration has s_r.
EDIT: That was the first thing that jumped out at me. Having looked a bit closer and realised the values are strings, I think fixing the variable name together with adding some escaped quotation marks as in Daniel A. White's answer will do the trick.

Finding commas in strings using JS

I'm trying to learn JS, so forgive me if you code makes the world explode.
Anyway, I am trying to make a tagging system interface similar to SOs. Where the user types in words and SO separates them on the comma (or on spacebar but I don't want that).
I am using jQuery to access the DOM (because it is so much easier), and I know there are various plugins that could do this and feed the homeless, but as I said I want to learn.
Here's what I have so far:
<input type="textbox" name="somebox" id"someid">
$(document).ready(function() {
var startPos = 0;
$("#someid").keyup(function() {
var inputText = $(this).val();
var commaPosition = inputText.indexOf(",", startPos);
var foundWords = [];
alert('Hello'); // even this doesn't work... why???
if (commaSearch !== '-1') {
// take the word:
foundWords.push(inputText.slice(startPos,commaPosition));
startPos = commaPosition + 1;
}
});
});
It can also be found here. Nothing seems to work, not even the alert. Any help would be grateful.
Problems:
Invalid HTML - you're missing an = in between id and "someid". This will make the alert() work.
Use String.split(',') rather than String.indexOf(','). To split and get rid of extra spaces: inputText.split(/\s*,\s*/g)
I also get Uncaught ReferenceError: commaSearch is not defined. – Felix Kling
Demo with fixes: http://jsfiddle.net/mattball/2sE8b/
Is this what you want?
DEMO
$(document).ready(function() {
$("#someid").keyup(function() {
var inputText = $(this).val();
var foundWords = inputText.split(","); // or as posted elsewhere split(/\s*,\s*/g)
if (foundWords.length>1) $("#tags").html(foundWords.join(";"))
});
});
You're going to laugh, but you didn't have the = infront of the id=someid :D http://jsfiddle.net/SuperPhil/9KVs4/2/
Is this what you want?
As Matt Ball pointed out, you're missing the = sign after id in your HTML. Next, you use commaPosition then commaSearch. Next, you use the not-identity operator with a number and a string, which will always return false.

Given the following JS/JQUERY, how to prevent it from being case sensitive

I have the following line of code which works well:
$("a.grouped_elements[href$=.jpg],a.grouped_elements[href$=.png],a.grouped_elements[href$=.gif]").fancybox();
Problem is, if the href is .JPG it doesn't work it only works with .jpg. How can I make the above case insensitive so either all caps or no caps or a mix all match for the file extension?
Thanks
Apparently, "a filter function should do what you need".
Since I am not familiar with filter functions, I would try something like the following, though I am not sure how efficient/inefficient it might be compared to filter functions mentioned above:
$('a.grouped_elements').each(function(){
var elem = $(this);
if(elem.attr('href').match(/(gif|jpg|png)$/i) != null) {
elem.fancybox();
}
});
(hasn't been tested but that's the general gist of it)
Using filter as also mentioned by #Ricket. The idea is to return true if we want the element to be included, false otherwise.
var fancyBoxable = $('a.groupd_elements').filter(function(){
var href = $(this).attr('href') || '';
return /(gif|jpg|png)$/i.test(href);
});
fancyBoxable.fancybox()
Ok this ended up working:
$('a.grouped_elements').each(function(){
var elem = $(this);
// Convert everything to lower case to match smart
if(elem.attr('href').toLowerCase().match('/gif|jpg|png/') != null) {
elem.fancybox();
}
});

Categories

Resources