I have formating (bold,italic,underline) list in multiselect dropdown. Based on user selection i need to create formating tag.
example: if user select bold and italic from dropdown i need to create tag like <b><i></i></b>
and if user select only italic i need to create tag like <i></i>
I have tried below:
<select id="multiple-select" multiple>
<option value="Bold">Bold</option>
<option value="Italics">Italics</option>
<option value="Underline">Underline</option>
</select>
<button id="Apply">Apply</button>
javascript code:
$("#Apply").click(function(){
var selectedtag = $("#multiple-select option:selected").val();
var selectedtag = $("#multiple-select option:selected").val();
for(var fr=0;fr<selectedtag.length;fr++)
{
var createtag = '<'+selectedtag[fr]+'></'+selectedtag[fr]+'>';
var createnode = document.createElement(createtag);
}
});
Expecting
based on input need to create tags
2
Answers
You will need to iterate over all selected options and construct the tags accordingly.
Directly assign tags as the values of corresponding options.
Use 2 variables to contact opening and closing of tags
Finally, combine them to get the final tag html.
Working snippet: