4

It's showing € instead of the € (currency sign) in the textarea. Does anyone have an idea why this is going wrong?

<?php
$currency = "&euro;"; //using php with other data from database

echo "<script>
      $('#share_button').click(function(e){
      // pass the currency to javascript and put in textarea shared with other on clicks
           $('#snsdescp').val(`".$currency."`);
      });</script>";
?>

 //shared textarea
<textarea class="form-control" name="message" id="snsdescp"></textarea> 
faris
  • 43
  • 5
  • 2
    The `.val()` method doesn't set html, so `"€"` isn't treated as an html entity, it's just treated as a plain string. You could use a "hack" like `.val($("
    ").html("€").text())` to get the actual Euro character.
    – nnnnnn Nov 24 '16 at 05:11
  • Because you are adding that as a value inside a `textarea`, not as HTML that would be parsed. – Spencer Wieczorek Nov 24 '16 at 05:11
  • [html_entity_decode](http://php.net/manual/en/function.html-entity-decode.php) could help, server side – Jaromanda X Nov 24 '16 at 05:22
  • don't need to hack it, use `html()` instead of `val()`. Like `$('#snsdescp').html('€');` – phobia82 Nov 24 '16 at 05:38
  • Just use the actual euro character `€`. Make sure the file is saved as UTF-8, served as UTF-8, and the HTML meta charset is set to UTF-8. –  Nov 24 '16 at 05:54
  • Don't show PHP, show JS/HTML. –  Nov 24 '16 at 05:59

4 Answers4

3

The val() method does no encoding/decoding, as a hack you can use the html() function for the encoding and then strip the text:

$('#share_button').click(function(e){
    $('#snsdescp').val($("<div>").html("&euro;").text());
});

Here is a working jsFiddle for your textarea.

Nick Vanderhoven
  • 3,018
  • 18
  • 27
  • That's probably not going to work very well if the text contains characters such as `<` which you want to interpret as themselves, not HTML. –  Nov 24 '16 at 05:56
0

You can set it by assigning &euro; to the innerHTML property of the textarea element:

document.querySelector('#snsdescp').innerHTML = '&euro;';

If you would like to use jQuery, you can simply call its .html() method:

$('#snsdescp').html('&euro;');
0

Well I am not getting why you storing in php variable, but probably you can store in javascript variable. It can help you out.

<?php
  $currency = "\u20ac"; //using php with other data from database
?>

 <textarea class="form-control" name="message" id="snsdescp"></textarea> 
  <br/><br/>
  <div id="share_button" >hello</div>//suppose your id is here

 <script>
    var value ="<?php echo $currency;?>"; //store php value in js variable
    $("#share_button").click(function(){

        $("#snsdescp").val(value);

     });
  </script>
shikhar
  • 110
  • 10
0

You can use Unicode for € \u20AC

$('#snsdescp').val("\u20AC");

Arun Killu
  • 13,581
  • 5
  • 34
  • 61