Is there any way in Javascript or jquery using which we can internally click the "Delete" button to delete some text in HTML web pages? I don't want my users to click the "Delete" button physically but I want to give them interface where when they click I will apply the "Delete" button functionality there.
Is there any way in Javascript or jquery using which we can internally click the "Delete" button to delete some text in HTML web pages? I don't want my users to click the "Delete" button physically but I want to give them interface where when they click I will apply the "Delete" button functionality there.
Share Improve this question asked Aug 22, 2014 at 6:17 ThompsonThompson 2,00010 gold badges37 silver badges59 bronze badges 5- Why would you want a user to delete your HTML text? – Richie Commented Aug 22, 2014 at 6:18
-
1
you could either clear the contents of an element out with
elem.innerHTML = ''
or using something likecontents.substring(0,contents.length-1)
you could truncate the last character off – haxxxton Commented Aug 22, 2014 at 6:22 - can you provide sample what you want? – Grundy Commented Aug 22, 2014 at 6:23
- This is the only thing which I want that I want internally do the functionality of Delete button inside a Text Editor that I have, on click on a custom option made by me. – Thompson Commented Aug 22, 2014 at 6:24
- 1 Perhaps this article could be useful to you. – Ja͢ck Commented Aug 22, 2014 at 6:27
4 Answers
Reset to default 2You can trigger it using the keydown
function:
$(function(){
var press = jQuery.Event("keyup");
press.ctrlKey = false;
press.which = 46;
$('#check').keyup(function(e){
alert(e.which);
}).trigger(press);
});
But this solution does NOT simulate a natural keypress event on the HTML element. This method only triggers the keyup event, it does not replicate the user going into the element and pressing that key. For doing that you can refer HERE.
But this dispatch event is also not globally supported.
I think applying the functionality of delete button on click of button can fullfill your requirement
Just write $("input").val("")
on click of button or follow the answer of @haxxton given above, OR SEE DEMO HERE
Further to my ment on the OP.
Assuming your HTML looks something like
<p id="deleteMe">This text should be deleted</p>
<button id="deleteButton">DELETE</button
Option 1
if your intention is to remove the entire text of an element you could use
var deleteButton = document.getElementById('deleteButton');
deleteButton.onclick = function(){
var deleteItem = document.getElementById('deleteMe');
deleteItem.innerHTML = '';
}
Option 2
However, if it is your intention to only remove one character per click you could use something like
var deleteButton = document.getElementById('deleteButton');
deleteButton.onclick = function(){
var deleteItem = document.getElementById('deleteMe');
deleteItem.innerHTML = deleteItem.innerHTML.substring(0, deleteItem.innerHTML.length - 1);
}
Please visit : Is it possible to simulate key press events programmatically?
you can use the keycode as 46 insetead of
character.charCodeAt(0)
Call internally to a event is simple with jQuery
:
$('#delete').click(); // call internally to 'Delete' button
It launchs the click event which contains your delete functionality.
See a demo more plete: http://jsfiddle/581env0c/2/