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 value='a'>a</option>
        <option value='b'>b</option>
        <option value='c'>c</option>
        <option value='d'>d</option>
    <select name='opt' id='opt2'>
        <option value='AAA'>AAA</option>
        <option value='BBB'>BBB</option>
    <input type='submit' name='submit' value='submit' onClick="return function1();">

<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.


What about this?

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


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("");

Answered By – Tobias Golbs

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published