2

I've been trying to get this idea I had together for a simple project i'm working on.

The result I would like is to have a button that when clicked adds the barebones tags of a HTML5 doc. From there, I'd like to be able to add other HTML tags to it as well such as, lists, paragraphs, headers,a page title etc. and have them be in their proper place so if I add a <p> tag it'll be added in the body however, when I'm adding the <title> it will be under the head.

The problem I'm running into is that I've figured out how to get the "tags" into the textarea using it's .val() selector, but it's technically text NOT actual HTML so, I don't know how I could add/remove other elements without somehow switching the text in the textarea to actual HTML and try append or prepend every time a button is clicked from there.

here's what I have so far for the jQuery...

$(document).ready(function () {
    $("button#setup").click(function () {
    var text_area = $("#code_window")
    $(text_area).val("<!DOCTYPE html>" + "\n" + "<html>" + "\n" + "<head>" + "\n\t\n" +        "</head>" + "\n" + "<body>" + "\n\t\n" + "</body>" + "\n" + "</html>");
    });
});

and here's everything together..

http://jsfiddle.net/fHg8H/

Anything you could contribute would be appreciated.

Thanks!

2

1 Answer 1

1

I would use .text(). Try this:

$(document).ready(function () {
    var text_area = $("#code_window");
    $("button#setup").click(function () {
        $(text_area).text("<!DOCTYPE html>" + "\n" + "<html>" + "\n" + "<head>" + "\n\t\n" + "</head>" + "\n" + "<body>" + "\n\t\n" + "</body>" + "\n" + "</html>");
    });
    $("button#add_p").click(function () {
        var txt = $("#code_window").val();
        var new_txt = txt.replace('</body>', '<p>' + '\n' + '</p>' + '\n' + '</body>');
        $(text_area).text(new_txt);
    });
});

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.