0

I have the following block

<span id="title"><select id="optionType"><option>choose option</option><option> hello</option><option>how are you</option></select></span> 

I want to remove the first option and need to get the all other options in new line, only the option text with out any tags . Is regular expression is the best one to use, or is thire any other better method to retrieve text in new line ? I tried the following but it can fail somtimes

Description= $('title').innerHTML;
var tmp = Description;

tmp = tmp.replace("--Choose an option--</option>",""); // remove first option
tmp = tmp.replace(/<\/option><option>/g, "<\/option>\n<\/option>"); 
tmp = tmp.replace(/(<([^>]+)>)/g,""); 
tmp = tmp.replace(/\n/g, "\n"); 
tmp = tmp.replace(/^[\s]*/gm, '');

return tmp;
YOU
  • 120,166
  • 34
  • 186
  • 219
kiran
  • 11
  • 1

3 Answers3

1

Try with jQuery if possible instead of regex.

It would be something like

$("#optionType>option:first").remove() //to delete first option
$("#optionType>option") //list of other options
YOU
  • 120,166
  • 34
  • 186
  • 219
0
(<option[^>]*?>([^<]+)<\/option>)
Raphael
  • 3
  • 1
  • 3
0

Have you considered just removing the option directly rather than fiddling with the HTML code?

Particularly easy, since you're using JQuery already:

$("#title option[value='--Choose an option--']").remove();

See also: Removing an item from a select box

Community
  • 1
  • 1
Spudley
  • 166,037
  • 39
  • 233
  • 307