Jump to content

Removing 0s from comma seperated value


dweb

Recommended Posts

hey all

 

i have a html form and my input field looks like;

<input id="fieldx" value="1,0,23,0,99">

the value changes each time, but that gives you an example

 

As you'll see, I have 0's in the input field

 

Inside a function, what i want to do is run the following

var elm = document.getElementById('fieldx').value;

then strip any 0's from that input field, and output the new value using

document.form.fieldx.value = stripped_zeros;

so the output would look like

<input id="fieldx" value="1,23,99">

can anyone help?

 

thanks

Link to comment
Share on other sites

var elm = document.getElementById('fieldx').value + ',';
elm = elmAry.replace('0,', '');
elm = elmAry.substr(0, elmAry.length-1)

@Pravin, the problem with the process you proposed is that if the first value was a 0, then it would leave a comma at the beginning of the string.

Edited by Psycho
Link to comment
Share on other sites

use javascript replace function

var elm = document.getElementById('fieldx').value;
elm.replace("0","");

after replacing "0" from string it will give output as "1,,23,,99", now replace ",," to blank

elm.replace(",,","");

try this

 

Thanks, I tried that and ended up with

function do_something()
{
var elm = document.getElementById('fieldx').value;
elm.replace("0","");
elm.replace(",,","");
document.form.fieldx.value = elm;
}

but that didn't seem to work, any idea why?

 

thanks

Link to comment
Share on other sites

but that didn't seem to work, any idea why?

 

And what did it do? Any errors? Whya re you referencing the field by the ID at the beginning and then through the form object at the end? Try

//Pass the field ID to the function
function do_something(fieldID)
{
    var fieldObj = document.getElementById(fieldID);
    var elm = fieldObj.value + ',';
    elm = elmAry.replace('0,', '');
    elm = elmAry.substr(0, elmAry.length-1)
    fieldObj.value = elm;
}

//usage
do_something('fieldx')
Link to comment
Share on other sites

var elm = document.getElementById('fieldx').value + ',';
elm = elmAry.replace('0,', '');
elm = elmAry.substr(0, elmAry.length-1)

@Pravin, the problem with the process you proposed is that if the first value was a 0, then it would leave a comma at the beginning of the string.

 

thanks for fu

 

 

 
And what did it do? Any errors? Whya re you referencing the field by the ID at the beginning and then through the form object at the end? Try
//Pass the field ID to the function
function do_something(fieldID)
{
    var fieldObj = document.getElementById(fieldID);
    var elm = fieldObj.value + ',';
    elm = elmAry.replace('0,', '');
    elm = elmAry.substr(0, elmAry.length-1)
    fieldObj.value = elm;
}

//usage
do_something('fieldx')

 

 

It just doesn't do anything, no errors, just dead, but i'll try your code above

 

Question, do I do the following

document.form.form_product_box_array2.value = fieldObj.value;

or do I use

document.form.form_product_box_array2.value = elm;

thanks

Link to comment
Share on other sites

nice one, that's pretty sweet. is there a way to make sure the value ends with a comma? and if not, add one on?

That removes the value and comma that goes with it.  But I did make one mistake.  That will replace like "50,23" and leave "523".

 

Try this.

 

str.replace(/^0,|,0(,)|,0$/g, '$1')
Edited by Xaotique
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.