sid606
sid606

Reputation: 335

geting tags from textarea with jquery

I want to get some HTML code that are in textarea apply some function to the code replace the src with other url and return the code back to the textarea when I click on button. I would like to do this with jquery.

<textarea  id="proverka"><div id='sd'> <img src="/images/panorami/53.jpg"></div></textarea> <input type="button" id="mes1" name="btn_cancel"
                value="change" />

Is this possible?

Upvotes: 0

Views: 433

Answers (2)

Dr.Molle
Dr.Molle

Reputation: 117354

You can create an jquery-object of the text inside the textarea, manipulate it and write the html back:

$('#mes1').click(function(){
var tmp= $('<root/>').append($('#proverka').val())[0];
    $('img',tmp).attr('src','another.url');
   $('#proverka').val($(tmp).html());
});

Upvotes: 1

Trike
Trike

Reputation: 508

One possible way to do this:

  1. Create a new element from a textarea value

    $('#dummy').html($('#proverka').val());

  2. Change the SRC attribute of the new element

    $('#dummy img').attr("src", 'newvalue');

  3. Retrieve HTML of the new element and put it back to the textarea

    $('#proverka').val($('#dummy').html());

Upvotes: 0

Related Questions