2

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?

Community
  • 1
  • 1
tejash patel
  • 591
  • 1
  • 7
  • 19
  • You can check it : [Double quotes inside html attrbute](https://stackoverflow.com/questions/3996653/are-single-double-quotes-allowed-inside-html-attribute-values). Use `encodeHTML` convert `'Pet Size Bed (40" X 30")'`. – Minh Nguyen Feb 08 '19 at 05:04
  • @MinhNguyen I used that but removed because performing many operations on that after this so I need to do lots of code for manage that. Thanks for your time. – tejash patel Feb 08 '19 at 06:05

2 Answers2

1

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>
Bharath
  • 335
  • 2
  • 8
  • It not right if option value doesn't like option text – Minh Nguyen Feb 08 '19 at 05:13
  • In that case ,You can try covering the value with single qoutes. – Bharath Feb 08 '19 at 05:16
  • @MinhNguyen Yes, you are right. But, in my case both are the same so no issue. – tejash patel Feb 08 '19 at 06:03
  • @Bharath Now I am using html() as you suggest. But here I get another issue. I will just update in question. – tejash patel Feb 08 '19 at 06:09
  • @tejashpatel As you mentioned in u r question, using '.text()' can be a quick fix. But escaping Double quotes or . Giving single qoutes inside double qoutes is a proper fix Check this Link:- https://www.w3schools.com/js/js_strings.asp – Bharath Feb 08 '19 at 07:35
0

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>';
}
Shail Paras
  • 1,125
  • 1
  • 14
  • 34