1

Is it possible to convert all div child information into XML or JSON using JavaScript?

$("#droppable").droppable({
drop : function(event, ui) {
    var id = $(ui.draggable).attr("id");
    var cloneObj = $((ui.draggable).clone());
    $(cloneObj).removeClass("draggable ui-draggable");
    if (id === "txt") {
        inputOBj = document.createElement("input");
        inputOBj.setAttribute("id", "txt" + i);
        $("#droppable").append(inputOBj);
    } else if (id == "combo") {
        inputOBj = document.createElement("select");
        inputOBj.setAttribute("id", "select" + i);
        console.log("");
    }
  });
3
  • From what I understand html and xml are closely related. Any specific reason you would want to convert to one from the other? Commented Dec 24, 2012 at 12:41
  • thanks for reply. I want to generate html code using Drag -n-Drop in javascrip. I able to create the hmtl form in DnD but to save into seperate file i require xml/json or any which can be usefull to save the generated code into seperate file. Commented Dec 24, 2012 at 12:45
  • This answer might help stackoverflow.com/questions/10866997/… Commented Dec 24, 2012 at 12:58

2 Answers 2

10

I believe you can use XMLSerializer to do this.

var yourString = new XMLSerializer().serializeToString(cloneObj[0]);
Sign up to request clarification or add additional context in comments.

Comments

1

there is property called outerHTML. It Sets or retrieves the object and its content in HTML. U can use it in following way. e.g:

$(document).ready(function() {  
    $('#p').click(function() {
        alert($('#p')[0].outerHTML);
    }); 
});

tip: p is your any tag ID in body of page.

1 Comment

Good Answer... it also work. but what is difference between above 2 answer?

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.