Question

I have the next string variable which contains the next string

var content = '<div id="TypeModifiedProduct" class="form-group WSUage2">
  <label for="TypeModified">Type</label>
  <span class="pull-right alignText" style="margin-right: 40%;">
    <input type="checkbox" id="UseTypeModified" name="UseTypeModified" > Use option
  </span>
  <select name="TypeModified" id="TypeModified" class="form-control" style="width:40%" >
    <option value="Maintenance - Stand Alone form" >Maintenance - Stand Alone form</option>
    <option value="Maintenance Plus - Stand Alone form">Maintenance Plus - Stand Alone form</option>
  </select>
</div>';

Now I want for example search for the second option

<option value="Maintenance Plus - Stand Alone form">Maintenance Plus - Stand Alone form</option>

And change it for beign selected option

<option value="Maintenance Plus - Stand Alone form" selected >Maintenance Plus - Stand Alone form</option>

Then finally return this new string with the change in the option element.

Thanks in advice

Was it helpful?

Solution

Since you are looking for a string as the result

var $div = $('<div />',{html: content});
$div.find('option[value="Maintenance Plus - Stand Alone form"]').attr('selected', 'selected');

content = $div.html();

console.log(content)

Demo: Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top