Replace Submit Button Value With Ascii Character
I'm trying to replace the 'save' value of the submit button with an ASCII character code like (), except it's displaying the literal code instead of its translation. $
Solution 1:
Just use the character in the value, like this:
$("#myform").on("submit", function(){
$("input#commit").attr('readonly', true).val("—");
});
or alternatively, use the unicode value like this:
$("#myform").on("submit", function(){
$("input#commit").attr('readonly', true).val('\u2014');
});
Solution 2:
$("button[type='submit']").on("click", function(e){
$("button[type='submit']").attr('readonly', true).html("—");
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formid="#myform"><buttontype="submit">Save</button><form>
Solution 3:
Pretty much, what you need to do is create an element with the ASCII entity and read that element's innerText
.
$("#myform").on("submit", function(){
var d = document.createElement("div");
d.innerHTML = "—";
var ascii = d.innerText;
$("input#commit").attr('readonly', true).val(ascii);
});
For more information, see this answer: How do i unescape HTML Entities in JS? (change < to <)
Solution 4:
Tried using String.fromCharCode and Even though this works...Doesn't when setting the value???
alert(String.fromCharCode('151'));
When i try to assign the value through jquery or just using plain javascript. The character doesn't appear using String.fromCharCode('151')
//doesnt work
$("input#commit").val(String.fromCharCode('151'));
//doesnt work
$("input#commit")[0].value = String.fromCharCode('151');
//doesnt workdocument.getElementById('commit').value = String.fromCharCode('151');
Best way i found to work around is by creating a temporary div. Setting the html() and returning the text() using jQuery;
//works
$("input#commit").attr('readonly', true).val($('<div/>').html('—').text());
Post a Comment for "Replace Submit Button Value With Ascii Character"