/    Sign up×
Community /Pin to ProfileBookmark

String replace in javascript????

is there a string replace or explode function in javascript? I have a set of fields with the value of “19.99 , sku342342” and so forth. Is there any way to take this value and extract only the content before the comma?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 13.2005 — Several... Given:

str1 = "19.99,sku342342";

You can do this:

ary1 = str1.split(",");

which yields:

ary1[0] with a value of "19.99", and

ary1[1] with a value of "sku342342"
Copy linkTweet thisAlerts:
@vicpal25authorJun 13.2005 — I get a syntax error that says "str1.split" is not a function. I guess its not reading it. Here is my function;

onload = function CalcTotal () {

var e, i = 0;

while (e = document.forms[0].elements[i++]) {

if (e.type == 'checkbox') e.onclick = function () {

var e, i = 0, total = 0, subtotal = 0, taxdue = 0;

while (e = document.forms[0].elements[i++]) {

if (e.type == 'checkbox' && e.checked)

var str1;

str1 = Number(e.value)

ary1 = str1.split(",");

alert(ary1[0])

subtotal += ary1[0] || 0;

taxdue=0.0825 * subtotal

total = subtotal + taxdue;

document.forms[0].elements.total.value = "$"+total.toFixed(2);

}

}

}

}
Copy linkTweet thisAlerts:
@JonaJun 13.2005 — [font=Trebuchet MS]You can’t split an integer. Convert it to a string, split it, and then convert the part(s) you want back into an integer (or floating point number, whichever).[/font]

<i>
</i>str1 = String(e.value);
ary1 = str1.split(",");
ary1[0] = Number(ary[0]);
Copy linkTweet thisAlerts:
@phpnoviceJun 13.2005 — I get a syntax error that says "str1.split" is not a function.[/QUOTE]
You have this sequence:

var str1;

str1 = Number(e.value)

ary1 = str1.split(",");

alert(ary1[0])

Change that to this:

ary1 = e.value.split(",");

ary1[0] = Number(ary1[0])

alert(ary1[0])
Copy linkTweet thisAlerts:
@vicpal25authorJun 13.2005 — nice..yup that did it..thanks you guys...that split() rocks ?
Copy linkTweet thisAlerts:
@phpnoviceJun 13.2005 — Cheers.
×

Success!

Help @vicpal25 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...