0
<select data-placeholder="Please select a payment method.." style="width:50%;" class="chosen" onchange="currencyChange(this.value)">
    <option value=""></option>
    <optgroup label="Cash">
        <option value="Paypal">Paypal</option>
        <option value="Bitcoin">Bitcoin</option>
        <option value="Western Union">Western Union</option>
        <option value="Delivery">Delivery</option>
    </optgroup>
    <optgroup label="Ingame Currency">
        <option value="RS3 GP">RS3 GP</option>
        <option value="OSRS GP">OSRS GP</option>
    </optgroup>
</select>

<script type="text/javascript">
var sign = "null";
var placement = "null";
float budget = "null";
function currencyChange(data){
    if (data === 'Paypal') {
        sign = "$";
        placement = "before";
    }
    if (data === 'Bitcoin') {
        sign = "$";
        placement = "before";
    }
    if (data === 'Western Union') {
        sign = "$";
        placement = "before";
    }
    if (data === 'Delivery') {
        sign = "$";
        placement = "before";
    }
    if (data === 'RS3 GP') {
        sign = "M/GP";
        placement = "after";
    }
    if (data === 'OSRS GP') {
        sign = "M/GP";
        placement = "after";
    }

    if (placement === 'before') {                                                            
        document.getElementById("budget").value = sign + document.getElementById("budget").value;
    }

    if (placement === 'after') {                                                            
        document.getElementById("budget").value = document.getElementById("budget").value + sign;                                                            
    }
}
</script>

I am trying to change the currency type of another textfield id="budget" to add a currency symbol dependent on the choice of payment method..?

But I get no such action when selecting the payment type.

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
Daniel Watson
  • 225
  • 1
  • 3
  • 10

4 Answers4

2

remove this, Its works

float budget = "null";

And,

add below select

<input type="text" id="budget">

Edited

        if (placement === 'before') {                
            toreplaceaft=document.getElementById("budget").value;   
            toreplacebef=toreplaceaft.replace("$","");
            toreplace=toreplacebef.replace("M/GP","");
            document.getElementById("budget").value = sign + toreplace.replace("$","");
        }

        if (placement === 'after') {     
            toreplaceaft=document.getElementById("budget").value;   
            toreplacebef=toreplaceaft.replace("$","");
            toreplace=toreplacebef.replace("M/GP","");
            document.getElementById("budget").value =  toreplace+ sign;                                                            
        }
Punitha Subramani
  • 1,467
  • 1
  • 8
  • 6
  • Well, yes, the syntactically invalid construct caused the function execution to be aborted (as one can see in a browser’s console log). The code still isn’t functionally correct, since changing the selection in the dropdown causes a currency symbol/abbrev to be added, without removing the old symbol/abbrev first. So you would get e.g. $$$42M/GP. – Jukka K. Korpela Sep 01 '14 at 12:56
  • After tweaking the toreplace.relace for the 'after' if statement I got it to function correctly. Thank you for your answer. – Daniel Watson Sep 01 '14 at 14:41
1

Try this fiddle it will not accepting double sign.

http://jsfiddle.net/pee7d6qr/1/

   if (placement === 'before') {  
    if(document.getElementById("budget").value.indexOf("$") == -1){
        document.getElementById("budget").value = sign + document.getElementById("budget").value;
    }
    else {
        document.getElementById("budget").value = document.getElementById("budget").value;
    }
}

if (placement === 'after') {     
    if(document.getElementById("budget").value.indexOf("M/GP") == -1){
        document.getElementById("budget").value = document.getElementById("budget").value + sign;     
    }
    else {
        document.getElementById("budget").value = document.getElementById("budget").value;     
    }
}
0

Now you need to strip those "$,M/GP" stuff from the string

With little help from stackoverflow:

value = document.getElementById("budget").value;
value = value.replace(/\D/g,''); //strip non-numeric characters from string

Then

if (placement === 'before') {                                                            
    document.getElementById("budget").value = sign +" "+ value;
}

if (placement === 'after') {                                                            
    document.getElementById("budget").value = value +" "+ sign;                                                            
}

jsFiddle

Sams
  • 684
  • 1
  • 8
  • 14
0

Remove the float variable in javascript

Budget Input element is missing

<input type="text" name="budget" id="budget" value=>

Use the below code

if (placement === 'before') {                                                            
    document.getElementById("budget").value = sign + document.getElementById("budget").value.replace(/[^0-9.]/g,'');
}

if (placement === 'after') {                                                            
    document.getElementById("budget").value = document.getElementById("budget").value.replace(/[^0-9.]/g,'') + sign;                                                            
}
sakthivel
  • 86
  • 1
  • 1
  • 9