0

i have the following javascript how and where to set so when it will get the data and shows in a textbox it will show only numeric values:

<script type="text/javascript">
function showData($sel){
   var str='';
   document.getElementById("demo").innerHTML;
   for (var i=0;i<sel.options.length;i++){
           str+=(str!='') ? ', '+sel.options[i].value : sel.options[i].value;
       }
   }
   sel.form.selectedFruits.value = str;
}
</script>

i have multiple select dropdown and it has multiple values like Staff No and email so i dont want to show email in text box only staff no and even i dont want to remove email from values.

it is working fine except what i want to do :D

usman610
  • 479
  • 1
  • 5
  • 22

3 Answers3

2

A simple solution, if you want to get only numbers from a string (or html in your example), will be :

var str= document.getElementById(id).innerHTML;
return str.replace(/[^0-9]/ig, '');

See this jsfiddle.

soyuka
  • 8,839
  • 3
  • 39
  • 54
0

if I've gotten the point correctly try something like this

<script type="text/javascript">
function showData($sel){
   var str=[];
   document.getElementById("demo").innerHTML;
   for (var i=0;i<sel.options.length;i++){
           str[i]+=sel.options[i].value.replace(/\D/g, ''); // remove everything except digits
       }
   }
   sel.form.selectedFruits.value = str.join();
}
</script>
i100
  • 4,529
  • 1
  • 22
  • 20
  • thanks your given code is not working not showing anything but i got an idea from your code and its working now only i have changed only one line in my code and i got it working: str+=(str!='') ? ', '+sel.options[i].value.replace(/\D/g, '') : sel.options[i].value.replace(/\D/g, ''); – usman610 Aug 23 '13 at 12:49
  • 1
    yes I've missed the += should be str[i]=sel.options[i].value.replace(/\D/g, ''); and I'd prefer to put values by document.getElementById("selectedFruits").value = str.join(); – i100 Aug 23 '13 at 14:18
0
<script type="text/javascript">
function showStaffno(sel){
   var str='';
   document.getElementById("demo").innerHTML;
   for (var i=0;i<sel.options.length;i++){
       if (sel.options[i].selected){
           str+=(str!='') ? ', '+sel.options[i].value.replace(/\D/g, '') : sel.options[i].value.replace(/\D/g, '');
       }
   }
   sel.form.selectedFruits.value = str;
}
</script>
usman610
  • 479
  • 1
  • 5
  • 22
  • this is the working code / answer of this question with the help/ idea from the code of Mr. i100 ... – usman610 Aug 23 '13 at 14:03