Gaurav
Gaurav

Reputation: 131

update textarea value using jQuery

I'm using Bootstrap WYSIHTML5 html editor in my form like

 <input type="text" name="product" id="product">
 <textarea name="description" id="description" class="textarea" placeholder="Place some text here" style="width: 100%; height: 200px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;">

 </textarea>
 <input type="product_code" name="product_code" id="product_code">

and using Ajax to fetch product details when product is entered in input field and want to update textarea with description

  <script type="text/javascript">
    $(document).ready(function($){
      $('#product').autocomplete({
        source:'<?php echo Router::url(array("controller" => "Products", "action" => "search")); ?>',
        minLength: 1
      });


      $('#product').blur(function() {
        var p_term = $(this).val();

        if (p_term) {
          var dataString = 'term='+ p_term;
          //alert(dataString);

          $.ajax({
              dataType:'json',
              type: "POST",
              url: '/products/ajax-search-product',
              data: dataString,
              cache: false,
              success: function(data) {

                /* set values to input field */
                $('#description').val(data['description']);
                $('#product_code').val(data['product_code']);

              },
              error: function(xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
              }
          });
        }
      });

    });
</script>

But this code is not updating values of textarea field while $('#product_code').val(data['product_code']); is working fine.

I tried to print value of textarea by alert($('#description').val()); and it is working and printing prefilled value in textarea field.

Edit 2

Console output : console.log(data);

Object { 
  id: 1, 
  product_code: "765457", 
  SKU: "", 
  title: "yuphoria screen guard", 
  description: "yuphoria screen guard", 
  short_description: "yuphoria screen guard", 
  15 more… 
}

I tried with this

$('#description').val(data['description']);    // this not working
alert(data['description']);                    // this working

There is only one id="description" and is nowhere else using.

Upvotes: 2

Views: 2093

Answers (1)

yogatofan
yogatofan

Reputation: 1

I found this solution, I hope it will help.

$('iframe').contents().find('.wysihtml5-editor').html('your new content here');

Upvotes: 0

Related Questions