2

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.

Thompson
  • 1,954
  • 10
  • 33
  • 58
  • Why would you want a user to delete your HTML text? – Richie Aug 22 '14 at 06:18
  • 1
    you could either clear the contents of an element out with `elem.innerHTML = ''` or using something like `contents.substring(0,contents.length-1)` you could truncate the last character off – haxxxton Aug 22 '14 at 06:22
  • can you provide sample what you want? – Grundy Aug 22 '14 at 06: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 Aug 22 '14 at 06:24
  • 1
    Perhaps [this article](http://www.dotnetvishal.com/2012/11/replace-selected-text-in-textbox-using.html) could be useful to you. – Ja͢ck Aug 22 '14 at 06:27

4 Answers4

2

You 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

MARSHMALLOW
  • 1,315
  • 2
  • 12
  • 24
Roshan
  • 2,144
  • 2
  • 16
  • 28
1

Further to my comment 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);
}
haxxxton
  • 6,422
  • 3
  • 27
  • 57
0

Please visit : Is it possible to simulate key press events programmatically?

you can use the keycode as 46 insetead of

character.charCodeAt(0)
Community
  • 1
  • 1
Madhavan Malolan
  • 719
  • 6
  • 24
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 complete: http://jsfiddle.net/581env0c/2/

SnakeDrak
  • 3,406
  • 4
  • 28
  • 41