jQuery - find a label in a string by its innerHTML and add an attribute

advertisements

I have the following piece of code in my HTML file

   <select>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <!-- ......... -->
        <option>50000</option>
   </selcet>

using jQuery I get

      var subHTML=$("select").html();

Can I find an option tag by its innerHTML in subHTML string, insert attribute "selected" and then put the edited string back into the select tag

      $("select").html(editedString);

Edit

I tried

  var strel=$(subHtml).find('option:contains("123")').attr('selected','selected');
  $("select").html(strel);

but it adds only one option to the select tag.


This should work. (Not tested).

var to_replace = $("select").find('option:contains("The text i want to find")');
to_replace.attr("selected", "selected");
to_replace.text("The new text");