treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Jqurey append html as text to textarea

  • Hi

    I am trying to make a link which once clicked will add text to the text area/field. The text needs to be pure text (HTML not translated).

    So I got two pieces of jquery code which I struggling to make work together to achieve what I want.

    To make HTML be plain text I have this code:

    var text = document.createTextNode('<p>MyText</p>');
    document.body.appendChild(text);


    And to append the
    <p>MyText</p>
    to the text area I have a rough idea, like this:

    $("#answer1").click(function(){
    $('ul li').append('&#10003;');
    });


    (found on internet)

    Now can anyone help me structure this code to function the way I want, please?

    Regards
    Aleksander
  • $(function() {
    $('#answer2').click(function() {
    var content = '<a href="#">&#10003; TADAAA!</a>';
    $('textarea').append(
    content
    .replace(/\&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    );
    });
    });


    http://jsfiddle.net/tovic/kMmVU/3/
  • @Hompimpa Thank you!! worked perfectly.
  • ohh, but it doesn't work once you type something in... any ideas?
  • Try this:


    $('#answer2').live('click', function(){});


    instead of this:


    $('#answer2').click(function(){});