Replacing characters in a string

2

I have a string to be passed by the form and would like to replace some of its characters so that it is in numeral form:

if(form1.autonomoBonusBruto.value.includes("R$")){
            form1.autonomoBonusBruto.value.replace("R$","");
            alert(form1.autonomoBonusBruto.value);
        }
    
asked by anonymous 15.12.2016 / 02:10

2 answers

2

The problem is that you are not assigning the value to the variable. The replace() does not change the variable, it manipulates the value and returns this new value. If you do not save it somewhere, it is lost. This works:

if (form1.autonomoBonusBruto.value.includes("R$")){
    form1.autonomoBonusBruto.value = form1.autonomoBonusBruto.value.replace("R$", "");
    alert(form1.autonomoBonusBruto.value);
}
<form name="form1">
  <input name="autonomoBonusBruto" value="R$">
</form>
    
15.12.2016 / 02:28
0

Dude, there's no problem with this code. Your variable contains "$" and you replace it with "". What would you like to appear in alert ()?

if (form1.autonomoBonusBruto.value.includes("R$")){
    form1.autonomoBonusBruto.value = form1.autonomoBonusBruto.value.replace("R$", "");
    alert(form1.autonomoBonusBruto.value);
}
<form name="form1">
  <input name="autonomoBonusBruto" value="R$10,00">
</form>
    
15.12.2016 / 22:38