Updating String/array Items
Oct 16, 2007
I'm not entirely familiar with the norms and standard libraries of
JavaScript so if the answer to this is yesterday's news, please ignore.
I'm trying to write a simple text formatting function to make headings
proper case -- i.e. first letter of words capitalized.
I first tried this...
function inProperCase(s)
{
var result, i, k;
i = 0; result = s.toLowerCase();
while ((k = result.indexOf(' ', i)) -1) {
if (k - i 0) {
result[i] = result[i].toUpperCase();
}
i = k + 1;
}
if (result.length - i 1) {
result[i] = result[i].toUpperCase();
}
return result;
}
...but it didn't work. To my amazement, you cannot set a character in a
string by accessing that character with an index, like so:
s[i] = ch;
Must be because JavaScript strings are "Immutable" or "EtchedInStone" or
something. (Just kidding, no flames, please. I'm sure it is more
efficient.)
The only thing I could come up with was this (with some details, like the
"compound" while predicate, modified into a more readable form after being
called a "C-bigot" by a friend :)) which seems pretty heavy-handed with all
the string slicing and concatting:
function inProperCase(s)
{
var result = s.toLowerCase(),
i = 0, k = result.indexOf(' ', i);
while (k -1) {
if (k - i 0) {
result = result.substring(0, i) +
result[i].toUpperCase() +
result.substring(i+1, result.length);
}
i = k + 1;
k = result.indexOf(' ', i);
}
if (result.length - i 1) {
result = result.substring(0, i) +
result[i].toUpperCase() +
result.substring(i+1, result.length);
}
return result;
}
You call this as...
inProperCase(" madam i'm adam ");
...and you get...
" Madam I'm Adam "
Any suggestions for making this a bit more elegant?
BTW You may point me to a "library" with a super-efficient implementation
of this function, but that is not the point. My issue is, how do we update
single characters in a string (or single items in an array) in JavaScript?
Is it something like...
s.wouldYouCareToUpdateThisChar(i, ch); // :D (just kidding, calm
down...)
-or-
s.setCharAt(i, ch); ...
View 20 Replies
ADVERTISEMENT
May 23, 2005
I have a form for taking customer details. Part of the form will allow the user to select some upgrades for their selected product. The upgrades will be displayed in a drop down list with their additional cost. The total cost of the product being ordered will be displayed on the page.
Could someone please advise me on the best way to automatically update the total cost displayed on the page each time an upgrade is selected? I am using PHP for the site but I'm assuming that javascript is the best choice for this.
View 3 Replies
View Related
Jan 27, 2010
Is it possible to break apart a string into characters, be it a word or a sentence, and store each individual character in an array?
View 11 Replies
View Related
Jul 23, 2005
I have a string that contains n items. Each item start with a '@' and the
item itself does not contains the '@a'.
For example the string looks like: "@one@two@three@four"
I have to output this string as "one, two, three and four".
So in fact the first '@' can be removed, the next except the last replaces
by ", " and the last one by the word "and ".
Is there a simple way doing this?
View 13 Replies
View Related
Feb 5, 2009
I have a site that uses bullet points. I am able to add and delete these bullets as needed. The problem i face is every time I add a new bullet and update the page all the bullets that are already there are saved again. for example lets say I have in my list:
[Code]...
So I was thinking that I could get an array of all the existing bullets and then say if the new bullet is not part of the array update it. If anyone else has any other ideas of how i could make this work I am glad to hear them. If you need code just let me know.
View 3 Replies
View Related
Nov 29, 2006
Right now, the array items print out without spaces like "Jane,John,Mary,Sue". I need to be able to work with the items individually so I can format them so they read "Jane, John, Mary and Sue". I thought I'd be able to do something like v[i] + ", " but I get an "undefined" when I try to alert for v[i]. Can someone point me in the right direction? Code:
View 17 Replies
View Related
Jun 29, 2011
What am I doing wrong I want to add an Array to a string, but the alert won't write the array?
Code JavaScript:
var placestosee = ['newyork','people','laundry','stores'];
alert('One place to see is the'+'placestosee[2]'+);
View 4 Replies
View Related
Mar 18, 2011
I get strings in this format:
([{"result":"-1","message":"Error","target":""},{"result":"-2","message":"Access Denied","target":""}])
this string will evaluate to a 2D array if I do this:var array = eval(string);But eval is not very safe.Is there a way to achieve the same effect (turn that string into 2d array) without using eval function?And I have no control over what string I am given, so will have to process this kind of a string =Right now I am thinking of writing a custom function which will split the string and then split again etc, but is there already something that can do this job?
View 5 Replies
View Related
Apr 17, 2009
I have a script to display 2 dropdown lists. Selecting the first list will populate the second with items. But what I want then is, items in the second are already in a array (this works) they also have a code and this code I want to display in a text input, when selecting an specific item in the second list. (I can display the item selected in the second list (but want the code behind that item to be displayed in the text field). Function assignprojectnum shows the item in the text field now.
Here is the code I use.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
[code]....
View 3 Replies
View Related
May 11, 2010
My website has a flash animation I'd like to replace with a jQuery animation [URL]. I'm trying to create a dynamic statement to grab an array of words from an XML file and loop through replacing the word in a <span> with a random item. Would like it to loop continuously while the page is displayed. The HTML statement is:
<p style="text-align: center; font-size: 2em; font-weight: bold; margin: .5em,0,.5em,0;">
<span>Shred your </span><span id="indexShredItem">paper</span><span>, not your nerves.™</span>
[Code]....
It works great once, but I can't get it to loop. I've tried "for" and "while" loops (no loop attempt is in the script above).
View 2 Replies
View Related
Aug 16, 2010
I want to create an array of list items that will shuffle randomly when a user refreshes the page. I also only want to display 3 items at at time, but when the browser refreshes it will show 3 random ones each time.
I've seen shuffle plugins, but they work if someone clicks on a button to shuffle the items, which I don't want to do. I want it to shuffle randomly on reload of the page.
View 8 Replies
View Related
Feb 18, 2010
I want to remove items in an array by passing index values. The index values may have 1 or more values. For example, i have the following array
Code:
var arr1=new Array("aa","bb","cc","dd","ee","ff");
var index = new Array();
index[] = 3;
index[] = 5
[Code]...
View 2 Replies
View Related
Feb 3, 2009
I am dynamically creating page elements by looping through an array and adding them to the DOM. Each element has an action that should be performed when it is clicked. This action (or a reference to it) determined by the current array value as well. Because the code called in the onclick handler is not executed until the event fires, the value of the current array item (at the time the onclick handler is attached) is not preserved resulting in the expected behavior not happening.Here is an extremely simplified example of the problem I'm facing:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
[code]....
View 2 Replies
View Related
Aug 15, 2011
In our ordering system, we have to type in various values for several items at a time. This means in one single form, for one order, we could be typing in a batch number 4 or 5 times. I can valiadate this easy enough with Javascript when there is only 1 line. But what if there is more than 1?
With PHP you add [] to the end of the form element's name. But how do I get javascript to work with this?
Code:
Basically, the batch number, expiry date and pallet/box number is mandetory. If they arn't entered, it needs to fail (return false). It doesn't seem to be doing anything, just moving on to the next part which is checking other parts of the form which isn't repeated.
View 2 Replies
View Related
Jan 5, 2010
How I can convert a string to a json array.
start code:
The problem is that .css treats snip[1] as a string but I need it to handle it as a json array.
Bad: .css
Good: .css
View 3 Replies
View Related
Aug 30, 2010
I have an array like this:
I need to compare these array items to my site div id's and make an onblur functionality that changes the input field value.
It does not change the selected input field value(this.value). And it only alerts when blurring from the input field that is first in the array(id1). If i click to the input field id="id2" it does not alarm?
View 2 Replies
View Related
Apr 27, 2010
I have an array:
I have a string:
I would like to use the value of array_string (array_123) as an array to parse out the values of it. But this isn't working:
View 1 Replies
View Related
Jun 12, 2007
I need some advice regarding wrote a javascript function.
The function purpose is to check the variable "selectedSeat",
for e.g if the selectedSeat value is
var selectedSeat = "A:01|A:03|A:05|B:01|B:02|B:03";
var selectedArray = selectedSeat.split("|");
because row A is not in sequence , i will display alert box to
user,but row B is in sequence is ok.
Main purpose of function is check if the row is in the sequence.
I have been cracking my head about this , anyone have suggestion is
much appreciated.
View 3 Replies
View Related
Apr 26, 2011
So i've written up the code to do a lightbox esque overlay image gallery. everything is working smooth and fine. The only concern is that I have the following:
lets say that I have a page that has multiple galleries. Obviously I would have to make each literal array with the gallery data different names, ex:
var _gallery1 = {
'images': [
{ 'id' : 1, 'src' : "" }
]
[Code]....
obviously "galVariable" would be undefined... as i'm trying to get "_gallery2.images" but trying to use "galVariable" to point to "_gallery2"
I'm familiar with solving a problem like this in PHP, not so much javascript. is there a JS function that can be used to ensure that it's using whatever "galVariable" equals to, and then that array's content is? Or is there a different way to go on about this.
View 3 Replies
View Related
Apr 26, 2011
I have code...
how can I convert these to an array so I can loop through the values?
View 2 Replies
View Related
Feb 27, 2010
What I'm trying to do, I think, is to use elements of an array as a part of an object. Here's a lil' code snippet:
Code:
I'm trying to insert each of the elements of the katMusik-array into the checked-line, thus checking the checkboxes with those names. However, it doesn't seem to work.
View 5 Replies
View Related
May 7, 2011
I am getting a string with the value of the clicked button from an array and some other select field values that are listed in the same set of mysql results as the clicked button.At the end of the script the page is redirected to in the script mentioned page with following string:process.php?value=1&bs=2&ad=3&ns=4&wt=5&in=6
Is it possible to make locationstring variable global and in a single line in order to be able to use the full string in other scripts or is there another solution.
View 1 Replies
View Related
Mar 9, 2010
I'm trying to turn an associative array key into a string. I know I can do that in php with a foreach loop like foreach ($array as $key => $value) and then I can do whatever I want with the key. I want to be able to loop through a javascript array using a for in loop and test to see if the key matches part of the name attribute of some inputsIs there a way to do this? I looked online and I found a way to turn a string into a variable name but not the reverse
View 2 Replies
View Related
May 24, 2011
I need to convert a string
lalaw|lalaw1|lalaw2|lalaw3
in to Array
The values I have in variable "tables".I want to create variable list which takes values from "tables".Than I want to split this, and put each value in to new array: I've started with:
var list = "tables";
var listArray = list.split("|");
for(i=0; i < listArray.length;i++)[code].....
//? how to put now values in to array?
View 8 Replies
View Related
Jun 12, 2007
if I have an array where the number are write in string format, how can I recognize when a string is in realty a number? example if I have this:
"50"
"house"
"light"
"100blue"
"yellow20"
"-100"
"20,5"
how can with a cicle to extract only:
50
-100
20,5
?
View 3 Replies
View Related
Sep 17, 2011
I've searched on the form and found answers but I cant get it fix for me. want to make a form with multiple rows with checkboxes. Firt I did it in PHP and it worked but I want to use Jquery. How can I get the checked checkboxes to a string?It thought it would be something like this (found it on this site):
var test = $(':checkbox').map(function() {
return this.id;
}).get().join(',');
[code]....
View 4 Replies
View Related