1

I want to create a live HTML/CSS preview on a page.

But the code will not be given using textareas. The code is going to be fixed in the page (div).

I want the user to be able to alter the code and that will reflect on the live preview box.I have created the page where you can change parts of the script text (for amateurs). You can preview that here : http://apolosiskos.co.uk/HTML-CSS-EDITOR/index3.html

01) The live preview does not work if I replace the textarea with a div.

02) Even if I use the textareas, the live preview does not work because in my HTML script I am using the codeand the xmp tags.

--> Snippet that works with a textarea but not with a div :

var wpcomment = document.getElementById('WPComment');

wpcomment.blur = wpcomment.onkeypress = function(){
    document.getElementById('prevCom').innerHTML = this.value;
}
#prevCom
{
  background:#124;
  color:#fff;
  min-width:20px;
  min-height:50px;
  font-size:25pt;
}
<textarea name="WPcomment" id="WPComment" placeholder="Add comments:">aaaaa</textarea>

<div id="prevCom"></div>
Datacrawler
  • 2,780
  • 8
  • 46
  • 100
  • 1
    Your fiddle has an error `Uncaught ReferenceError: $ is not defined`. As soon as I added jQuery it seems to work as expected – chazsolo Oct 20 '16 at 14:38
  • @chazsolo On jsfiddle I did not have this issue. I am not getting the error in the snippet now - did not notice before - and my main enquiry remains unsolved. I tried replacing the keyup with mousemove or click with no results. – Datacrawler Oct 20 '16 at 14:42
  • If it is a text area or some input like this, can't you use the onChange property ? (don't know if it is a JQuery ne though). I think it should do the trick no ? – Titouan Freville Oct 20 '16 at 14:44
  • @TitouanFreville I think the only solution is to use JS to copy the text (from the black boxes) in a hidden textarea. – Datacrawler Nov 01 '16 at 15:09

3 Answers3

1

with no success. Is there any other addEventListener() method I can replace keyup with?

Yes, blur

If you would like to add keydown events on a <div> element, you can do the following:

First, you need to set the tabindex attribute:

<div id="a-div" tabindex="1" />

Then, (2) Bind to keydown:

 $('#mydiv').bind('keydown', function(event) {
    //console.log(event.keyCode);
 });

If you would like your div to be "focused" from the start:

$(function() {
   $('#mydiv').focus();
});
George Katsanos
  • 13,524
  • 16
  • 62
  • 98
  • Τεστάρω σε λιγάκι. – Datacrawler Oct 20 '16 at 15:38
  • :) cool. I would write a more elaborate solution but I couldn't get to figure out what exactly was happening/supposed to happen in the test case/snipped/fiddle... – George Katsanos Oct 20 '16 at 16:15
  • if you write a more minimal test case I am sure we can figure out the problem. Just a box, one element, one function that handles the preview and thats all.. – George Katsanos Oct 22 '16 at 06:35
  • Done. I have changed the ticket. – Datacrawler Oct 24 '16 at 13:10
  • There were no errors in the console and the JSFiddle seems to work perfectly... ?what am I missing? – George Katsanos Oct 24 '16 at 13:33
  • I have a link on top. I gave a full description. I want this to work without the text areas. If you see my website, I have the code where there user can alter 3 parts in this code. This works fine. But I want to have a live preview and I cannot make it work. The live preview does not work at all when I use a div. – Datacrawler Oct 24 '16 at 13:50
  • aha, I think that's because these events are form related and not triggered for divs.. – George Katsanos Oct 25 '16 at 07:52
  • Το βασικο προβλημα ειναι οτι δεν λειτουγει το live preview. Ουτε με κουμπι, ουτε χωρις. Αυτο ειναι που θελω να λυθει (λειτουργει μονο με textareas και ανοιξα νεα ερωτηση και για αυτο). – Datacrawler Nov 01 '16 at 15:22
  • ακριβως, γιατι για να δουλεψει με divs η αλλα elements πρέπει να κάνεις αυτο που σου εγραψα στη λυση – George Katsanos Nov 01 '16 at 15:27
  • Αυτο ειναι το πρωτο version : https://jsfiddle.net/radomer/hbkkut92/2/ και εδω το δευτερο αλλα μαλλον κατι κανω λαθος απο αυτα που γραψες : https://jsfiddle.net/radomer/uvcu5h0L/1/ Αντεγραψα τον κωδικο στο JS μεσα στο function και επειτα δοκιμασα ξεχωριστα. – Datacrawler Nov 01 '16 at 16:40
0

You should place your preview code it within a function, then you can simply call it once the document has loaded.

https://jsfiddle.net/michaelvinall/4053oL1x/1/

The separate issue of your preview only rendering when you press the enter key, is because of the following if statement:

if(e.which == 13 && $(this).val().length > 0)

The e.which == 13 within your if is specifying that the code within the block should only be ran if the key pressed by the user was the enter key (code 13). By removing this portion of each if statement, any key pressed will execute the code within the block:

if($(this).val().length > 0)
  • I deleted the previous comment. Your jsfiddle above works. Thanks for that. How can I replace the textarea with a div? I also updated the question. – Datacrawler Oct 24 '16 at 13:12
  • I have updated the question and opened a new one. I think the only solution is to use JS to copy the text (from the black boxes) in a hidden textarea. – Datacrawler Nov 01 '16 at 15:08
0

Your function is call when keyup is trigger, but no after page load.

You must do it : Define function to call them when 2 different event are fired.

$(function() {
        function GetHtml(){
                var html = $('.html').val();
                return html;
            }

        function GetCss(){
                var Css = $('.css').val();
                return Css;
            }

        var previewRendering = function(){
                        console.log('kikou');
            var targetp = $('#previewTarget')[0].contentWindow.document;
            targetp.open();
            targetp.close();

          var html = GetHtml();
          var css = GetCss();

          $('body',targetp).append(html);
          $('head', targetp).append('<style>' + css + '</style>');

        };

        $('.innerbox').on("keyup",function(){
                previewRendering();
            });

        $(document).ready(function() {
            previewRendering();
        });

    });

This code can not work because load event is only compatible with this list of HTML tags: body, frame, iframe, img, input type="image", link, script, style

$('.innerbox').load(function()
  • Do you know how to replace the textarea with a div and make it work?@Mathieu Vedie – Datacrawler Oct 24 '16 at 13:11
  • See contenteditable attribute : http://html5doctor.com/the-contenteditable-attribute/ – Mathieu Vedie Oct 25 '16 at 19:46
  • I replaced the textareas in the snippet with div and the contenteditable="true" line and did not work. There must be a way to make this work without a textarea. – Datacrawler Nov 01 '16 at 10:58
  • 1
    It's not a simple replace that you must do. I don't know exactly how contenteditable work but I know that : it doesn't work like a textarea ( js event are different , etc ). Sorry i can't help you more :S – Mathieu Vedie Nov 01 '16 at 21:29