Mike Mike - 2 months ago 9x
HTML Question

How can i remove HTML nbsp tags from select options when assigning the text of option to hidden input

When the value is assigned to html hidden element it also assign nbsp tag which is in select statement. I don't want any html tags just only text

function setTextFieldA(ddl) {
var a = ddl.options[ddl.selectedIndex].text;
var res = a.slice(0,12);
document.getElementById('prod1').value = res;
/*document.getElementById('prod1').value = ddl.options[ddl.selectedIndex].text;*/

<select name="1" id="1" class="selectadd" onchange="setTextFieldA(this)" required >
<option value="99">Red Vein </option>
<option value="99">White Vein </option>
<option value="99">Green Vein </option>
<option value="109">Red Horned &nbsp; [Add $10]</option>
<option value="109">White Horned [Add $10]</option>
<option value="109">Green Horned [Add $10]</option>
<option value="109">Maeng da &nbsp; &nbsp; &nbsp; [Add $10]</option>
<input id="prod1" type = "hidden" name = "prod1" value = "Red Vein" />


If you use jQuery:

function setTextFieldA(ddl) {
  var res = $('option:selected',ddl).text().slice(0,12).trim();

Check this Fiddle