0

I write a form that inserts some xml tags into textarea. I use this function:

    (function ($) {
     addCustomTag = function (name, param, value) {
         var code = "<" + name + " " + param + "=\"" + value + "\">\n</" + name + ">";
         document.getElementById("codeArea").value += code;
     };
 })(jQuery);

How can I make that some other function will insert subtags into tags that were created before? XML code will never be used on server. All I need is to insert tex in specific line which is depends on what was on this line before not cutting it. Something like this:

    addCustomSubtag = function(name,param,value,parent) {
    document.getElementById("codeArea").selectionStart = document.getElementById("codeArea").value - parent.length;
    var code = "<" + name + " " + param + "=\"" + value + "\">\n</" + name + ">";
    document.getElementById("codeArea").value += code;
};

Javascript isn't necessary. It also can be written on jQuery. Thanks.

Timofey Trofimov
  • 1,134
  • 3
  • 12
  • 26
  • Do you mean you want to insert at caretPosition, if yes then this might help: http://stackoverflow.com/questions/946534/insert-text-into-textarea-with-jquery/946556#946556 – Sudhir Bastakoti Aug 14 '12 at 06:07
  • I think the question is not to append the html elements, instead insert inner tags of the already inserted xml tags. I am not sure whether the inserted xml tags will be treated as `tags`. If not the only way is string manipulation, regular expressions will be helpful in such case. – code-jaff Aug 14 '12 at 07:15
  • All I need is to insert text in specific line which is depends on what was on this line before not cutting it. – Timofey Trofimov Aug 14 '12 at 07:19
  • You can use jQuery for doing this. Please see the updated answer. – Diode Aug 14 '12 at 07:23

1 Answers1

1

You can any of these jQuery functions

Update:

Actually we can use jQuery DOM manipulation methods to manipulate XML also.

var xml = "<main/>";
alert(xml);                                  // <main/>
var $xml = $(xml).append($("<sub1/>"));
alert($xml.html());                          // <sub1></sub1>
$xml.find("sub1").append($("<sub2/>"));
alert($xml.html());                          // <sub1><sub2></sub2></sub1>
alert($xml.get(0).outerHTML);                // <main><sub1><sub2></sub2></sub1></main>
Diode
  • 24,570
  • 8
  • 40
  • 51