tejash patel
tejash patel

Reputation: 591

Set double quotes string in select option value from javascript/jquery and also get from javascript/jquery

My basic requirement is to set select option value using jquery and also want to get its value.

In my PHP code, I am creating options like

foreach ($option_value->values as $key => $opt_value) {
      $variants_options .= '<option value="' . $variant_val . '">' . $opt_value . '</option>';
}

From js add this value as,

jQuery("#section-" + row_id + " .variant_detail_section.addFromApi .option1_options").html(value.variants_options[value.variants_name[0]]);

Its look like Select option display as image

Now, I want to get value of selected

option1 = jQuery("#section-" + row_id + " .option1_options").find(":selected").val();

Here, I am getting half value

Getting: 'Pet Size Bed (40' instead 'Pet Size Bed (40" X 30")'

So, here the problem is How can I get double quotes string?

Is there any other way to set double quote string and get it from jquery or javascript?

UPDATE

Now I use html() for get text of selected option value.

Here my problem is I get html code of some special character like:

&amp; &lt;&gt; instead &,<,>

So How can I get special characters instead of its htmlcode?

Can I use text() or it create any issue?

Upvotes: 2

Views: 3417

Answers (2)

Shail Paras
Shail Paras

Reputation: 1163

Yes. You need to add slashes before the quotes:

In your PHP code use addslahes() and the you will be able to get the right value.

foreach ($option_value->values as $key => $opt_value) {
      $variants_options .= '<option value="' . addslahes($variant_val) . '">' . $opt_value . '</option>';
}

Upvotes: 0

Bharath
Bharath

Reputation: 365

You can use .html() for getting the text of selected Option.

Here's an example

$('#selBox').change(function(){
var selBox = $('#selBox').find(":selected").html()
console.log('Selected text value is ',selBox)
selBox = $('#selBox').find(":selected").val()
console.log('Selected actual value is ',selBox)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id='selBox'>
<option value='Pet Size Bed'>Pet Size Bed (40" X 30")</option>
<option value='M'>M</option>
<option value='L'>L</option>
</select>

Or You can also try covering the value with single qoutes

<option value='Pet Size Bed (40" X 30")'>Pet Size Bed (40" X 30")</option>

Upvotes: 1

Related Questions