georgia georgia - 1 year ago 55
HTML Question

jquery show data taken by select after removing parenthesis

I have an

and a
where I want to show a message depending on the select option. The
<select id='select1'>
shows options like
and I want only the
to show up in the
<div id='logId1'></div>
I wrote the following code


$("#select1").on("change", function() {
$("#logId1").html($(this).find(":selected").text(function(_, text) {
return text.replace(/\(.*?\)/g, "");


It shows what I want but the result
shows up like it is selected.
I'm a newbie in jquery so any ideas are welcome.

Answer Source

Currently you are modifying the text of selected option, when you use .text(fn) method.

You need to get the .text() and then perform the desired operation.

$("#select1").on("change", function() {        
    $("#logId1").html($(this).find(":selected").text().replace(/\(.*?\)/g, ""));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download