user2385241
user2385241

Reputation: 15

Inserting HTML character entities with JQuery

I'm trying to create a tool that allows me to insert different Spanish characters into a text box such as the inverted question mark. I've currently got the following, but this returns:

function (e){var n,r,i,o=this[0];{if(arguments.length)return i=x.isFunction(e),this.each(function(n){var o;1===this.nodeType&&(o=i?e.call(this,n,x(this).val()):e,null==o?o="":"number"==typeof o?o+="":x.isArray(o)&&(o=x.map(o,function(e){return null==e?"":e+""})),r=x.valHooks[this.type]||x.valHooks[this.nodeName.toLowerCase()],r&&"set"in r&&r.set(this,o,"value")!==t||(this.value=o))});if(o)return r=x.valHooks[o.type]||x.valHooks[o.nodeName.toLowerCase()],r&&"get"in r&&(n=r.get(o,"value"))!==t?n:(n=o.value,"string"==typeof n?n.replace(V,""):null==n?"":n)}}¿

HTML

<div class="container">
<textarea id="text"></textarea>
<div id="one">&#191;</div>

JS

$('document').ready(function() {
$('#one').click(function() {
    $('#text').val($('#text').val + '&#191;');
});
});

Any ideas on why I'm receiving this output? Also do HTML entities work within textboxes, if not how would I do so? No errors appear in the console.

JSFiddle

Upvotes: 1

Views: 230

Answers (2)

Bandydan
Bandydan

Reputation: 631

Try this one. Worked in your fiddle.

$(function() {
    $('#one').on('click', function() {
        $('#text').val($('#text').val() + "¿");
    });
});

Update: I posted spanish sign of question instead of its ASCII, changed $(document).ready to a shorter way $(function(), and added brackets after val().

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

$("#text").val is a function, which you are supposed to call.

That said, jQuery is massive overkill for such a trivial task:

document.getElementById('one').onclick = function() {
    document.getElementById('text').value += "\u00bf";
}

Note that \u00bf is the appropriate JavaScript escape sequence for ¿ - bf being 191 in hexadecimal.

EDIT: Alternatively, just hold Alt and type 0191 on your keypad. This will produce ¿ directly where you are typing, instead of at the end of the input which is what your JS does.

Upvotes: 2

Related Questions