Skip to content Skip to sidebar Skip to footer

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("—");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="#myform">
  <button type="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 = "&#151;";
  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 &lt; to <)


Solution 4:

Tried using String.fromCharCode and Even though this works...Doesn't when setting the value???

alert(String.fromCharCode('151'));

http://jsfiddle.net/k6pphr88/

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 work
document.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('&#151;').text());

http://jsfiddle.net/vtmryLbp/


Post a Comment for "Replace Submit Button Value With ASCII Character"