Mike Mike - 1 year ago 121
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" />

Answer Source

If you use jQuery:

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

Check this Fiddle

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download