Skip to content
Advertisement

Jquery removing commas

I’m using the following and it works, but the alerted value opt3 contains commas.

How do I remove them?

form method='get' action='form.php'>
    <select name='val' class='select' id='opt1' multiple>
        <option></option>
        <option value='a'>a</option>
        <option value='b'>b</option>
        <option value='c'>c</option>
        <option value='d'>d</option>
    </select>
    <select name='opt' id='opt2'>
        <option value='AAA'>AAA</option>
        <option value='BBB'>BBB</option>
    </select>
    <input type='submit' name='submit' value='submit' onClick="return function1();">
</form>

<script type='text/javascript'>
    function function1(){
        var opt1=document.getElementById("opt1").value;
        var opt2=document.getElementById("opt2").value;
        var opt3=$('#opt1').val();
        alert( opt3 );
        var response=confirm("Are you sure? option1="+opt1+" option2="+opt2+" option3="+opt3 );
        return response;
     }

If I select a c & d the alert shows a,c,d but I want it to show acd.

Advertisement

Answer

What about this?

var opt3=$('#opt1').val().replace(",","");

Update:

Ok. Like @jreziga mentioned. The return value of .val() will be an array. If you have a string the upper code example will do just fine. If you have an array like in your case you can do this:

var opt3=$('#opt1').val().join("");
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement