0

I have this obfuscated webpage that contains a text-area, When a user manually inserts text and presses Enter key while editing the text area an event that changes the DOM launches.

I need to pragmatically launch that event, I know how to get to the text-area itself (using getElementsByName) and I'm basically inserting text via textArea.value = '' How do I get that event to launch?

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
Tzvi
  • 343
  • 1
  • 2
  • 15

2 Answers2

1

Could you call a function when enter is pressed, and then also just call that function when you want to simulate enter being pressed?

element.addEventListener("keypress", function(event){
    if (event.keyCode == 13) {
        // Enter has just been pressed.
        enterPressed();
    }
});

function enterPressed(){
    // Do whatever you do when enter is pressed.
}

// Somewhere else off in your code when you want to "trigger" the enter press event:
enterPressed();
Josh Sherick
  • 2,161
  • 3
  • 20
  • 37
  • Yes, the problem is that I don't know how to invoke the already implemented event that occurs when enter is pressed while on the text area. This enables me to add code to the execution, but I also need to fire the original event... – Tzvi Dec 25 '13 at 09:32
  • As Alexander.Shtanko suggested there's always [jQuery.event.trigger()](http://api.jquery.com/trigger/) if you want to use jQuery. Looking at the jQuery source for that function, it seems that it just calls all of the handlers that would normally be called for the event you specify, so short of that there isn't a good cross-browser way to do this that I know of. Just curious, why do you need to actually trigger the event rather than using a method similar to that in my answer above? Maybe there's another way to solve your problem. – Josh Sherick Dec 25 '13 at 21:51
  • Because I have no idea how to recreate the functionality of the original event that happens while on the textarea. as the code is obfuscated and the source ain't available. – Tzvi Dec 26 '13 at 07:41
0

is this what you want

document.getElementById("id_of_your_textarea").addEventListener("keydown", function(e) {
    if (!e) { var e = window.event; }
    e.preventDefault(); // sometimes useful

    // Enter is pressed
    if (e.keyCode == 13) { document.getElementById("id_of_your_textarea").value = '' }
}, false);

EDIT: based on your comment, you can use the trigger

if you can use jQuery.

$('#textArea').trigger('keydown');
gaurav5430
  • 12,934
  • 6
  • 54
  • 111
  • Nope, The event already exists on the text area... I want to invoke it by command from outside. – Tzvi Dec 25 '13 at 09:02
  • Website doesn't and cannot have jquery, any idea on how to do this without it? – Tzvi Dec 25 '13 at 09:35
  • can you elaborate the scenario? on pressing enter key inside the textarea you need to clear it or something else – gaurav5430 Dec 25 '13 at 11:09
  • The website is already implemented, I want to create a JS call that launches an event (the webpage activates changes a div on the DOM) This event currently only occurs when a user presses presses a key while focused on the text-area. How do I recreate this behavior in code ? – Tzvi Dec 25 '13 at 12:15
  • in what scenarios would you need to call the event programmatically? – gaurav5430 Dec 25 '13 at 12:17
  • on button click ... I am creating a script that adds template values to the textarea - however this is different then the user actually typing it, also I wish I had the name of the function that is probably being launched to create this behavior then I could just execute it myself... but the code is really obfuscated – Tzvi Dec 25 '13 at 12:31