0

I create dynamically textarea element in that way:

    var myFrame = $("#description-frame").contents().find('body');
    myFrame.html('<textarea id="testowy_desc" rows="4" cols="50" style="border: 2px solid red;"></textarea>');

and after created this element, I want to use trigger - simulate click "k" using keyboard. Here is my code:

    if ( $('#testowy_desc').length > 0 )
    {
        $('#testowy_desc').focus().trigger({ type : 'keypress', which : 75 });
    }
    else
    {
        alert('element not exist');
    }

But I get: "element not exist". How I can do it?

Thanks.

UPDATE:

Here is part of HTML code:

 <iframe id="description-frame" class="valid" frameborder="0">
 <html>
 <head>
 <meta content="text/html; charset=UTF-8" http-equiv="content-type">
 <style>body {background: #FFFFFF;margin: 0px;padding: 0px;text-align:left;font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif; font-size: 0.875em;}.desc{color:gray;} body.error { background-color: #EFDBD6;} body.valid { background-color: #F0FFC1; color : #468847; } P {margin-top:0;margin-bottom:0}a{text-decoration: none;color:#000}
 </style>
 </head>
 <body id="rte" class="valid">
     to jest testowy opis
 </p>
 </body>
 </html>
 </iframe>

and this part code looks that:

enter image description here

Screen from firebug console:

enter image description here

Pavlo K
  • 151
  • 9
  • Mmm, you seem to creating the textarea in a frame and so I don't think $('testow_des') will find it in the frame. Something like $("#description-frame").contents().find('#testowy_desc'); might though. – Richard Housham Apr 30 '19 at 09:00
  • @RichardHousham i changed: `$('#testowy_desc')` for: `$('#description-frame').contents().find('testowy_desc')` but I still get: "element not exist" :/ – Pavlo K Apr 30 '19 at 09:03
  • '#testowy_desc' the # indicates an id – Richard Housham Apr 30 '19 at 09:05
  • ohhh, I see. Now it work - find an element but trigger doesn't work now. I want to simulate click: "k" by clickboard but now it doesn't work – Pavlo K Apr 30 '19 at 09:08
  • This might sound stupid but why not just add 'k' when creating the textarea. Or is this for something else. Another way of doing this would just be .val('k') – Richard Housham Apr 30 '19 at 09:13
  • I would look at this question/answer here https://stackoverflow.com/questions/832059/definitive-way-to-trigger-keypress-events-with-jquery and do it that way separating out the event – Richard Housham Apr 30 '19 at 09:15
  • Because I create a script - addon to mozilla firefox for some website. This website is using security - checking use keyboard. So I need to simulate one click to this element and then I will be able to use val() for this element. Anyone, why this code doesnt work now? :/ – Pavlo K Apr 30 '19 at 09:15
  • @RichardHousham I tried in that way as you sent url but it also doesn't work. – Pavlo K Apr 30 '19 at 09:17
  • Can you post the html (a cut down version of it) and I'll see what I can do. – Richard Housham Apr 30 '19 at 09:26
  • OK, I edited my first post. I added HTML code and screen. Thanks. – Pavlo K Apr 30 '19 at 09:35
  • I added also screen from firebug console if you need. – Pavlo K Apr 30 '19 at 09:46
  • Can you explain *why* you want to send a keystroke? Browsers have increased protection against such, as it is considered a security risk. So what is your use case? – trincot Apr 30 '19 at 10:10
  • When I try use val('text') on this website, I got an error - this site is checking if user use any keypress on keyboard. If not, I can't do my actions on website. So for verify, I have add one character using keypress into this iframe, then I will be able to use val('text) and it will be fine. – Pavlo K Apr 30 '19 at 10:14
  • It will not work. It is not even related to the `iframe`. Even if you have a simple `input` element on your own page, you'll not be able to send a simulated keystroke event. This was possible in the old days (as many Q&A on this site show), but security has been tightened a lot since then. – trincot Apr 30 '19 at 10:21
  • I've been doing some experimenting here but have a look at this. https://jsfiddle.net/rickj33/3fby0nzr/ The keyup/down events don't change the value. I'm going with the previous comments this feels like they won't work because your editing textarea values which seems to raise security flags. – Richard Housham Apr 30 '19 at 10:43
  • OK, but I want do it with firefox extension. so I can set in manifest.json special permissions. Maybe it will be help for my case? – Pavlo K Apr 30 '19 at 12:46

0 Answers0