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 I want it to show acd
Thanks
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("");
Try to replace all commas
var opt3=$('#opt1').val();
opt3 = opt3.replace(/,/g, "");
alert(opt3);
where ,
is the special character so escaped with /
and g
is to replace global occurances.
use replace method in jquery for removing all commas from the value
var opt3 = $('#opt1').val().replace(/,/g, "")
Try this:
var opt3=$('#opt1').val().replace(/,/g, '');//replace all comma
alert( opt3 );
When you use
$('#opt1').val()
it returns an array for multiple values.
Instead of .replace, you can try this :
$('#opt1').val().join(';')
You can change the separator ";", or use a loop like this :
var str = $('#opt1').val();
for (var i=0;i<str.length;i++){
alert(str[i]);
}