Skip to content Skip to sidebar Skip to footer

How To Update Code Inside Textarea With Jquery

I have a textarea that will contains a code entered by user and I want to get that code and scan it with jQuery to get the value inside a custom tag called setting then add this va

Solution 1:

Ok, here is idea based on, hm... simple search/replace with dynamic regex building (that part could/should be improved, maybe with better regex you can have better solution. I am bad at regex, but also, HTML shouldn't be parsed with regex:)). First part of your code is is as it was... i've changed second function.

$(document).on('click', '#update-code', function (){

    //template_code=$('#template-code').val();for(i=0;i<$('input:text').length;i++)  {

dynamic_regex="(<setting id='"+$('input:text').eq(i).attr('id')+"'>)(.*?)(</setting>)";

var regex = newRegExp(dynamic_regex, 'gm');

var subst = '$1'+ $('input:text').eq(i).val() +'$3'; 
$('#template-code').val($('#template-code').val().replace(regex, subst));

    }

});

So, number of text inputs is dynamic (tested on 3 setting tags!), and based on that number (length) and inputs id - we build regex and replace text of tags (line by line) with input values... of course, textarea val must be updated, properly.

Demo: http://jsfiddle.net/5zcxbaqf/

Solution 2:

You can try this code to update old data with the new one:

$(document).on('click', '#update-code', function (){
    new_data = $( "#settings-form" ).serializeArray();

    $.each( new_data, function( i, new_field ) {
        var xml = $($.parseXML(template_code));
        xml.find('setting#'+ new_field.name).text(new_field.value);
        template_code = (newXMLSerializer()).serializeToString(xml[0]);  
    });

    $('#template-code').val(template_code);
    $('#tab-1').removeClass('unactive');

    returnfalse;
});

Demo: http://jsfiddle.net/robinhuy/jd2hrgn8/

Post a Comment for "How To Update Code Inside Textarea With Jquery"