faris faris - 9 days ago 5
Javascript Question

HTML escape for euro character not being interpreted in textarea?

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>

Answer

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.

Comments