0

I am trying to fill a form with some preset values. The user chooses from a list of presets. This calls an ajax function which wil fill the form on success.

I thought everything works fine until someone told me it wouldnt work for him. So i tried it on my own devices and came to the conclusion there has to be something wrong with the .html() when I am trying to get HTML data inside a node.

This is the code:

$.ajax({
  type: "GET",
  url: "something.php",
  dataType: "xml",
  cache: false,
  success:  function(xml){
    var title = $(xml).find('title').text();
    $('#event-title').val(title);

    var description = $(xml).find('description');
    alert(description.html());
  },
  error:    function() {
    alert("An error occurred while processing XML file.");
  }
});

xml var could look like this:

<?xml version="1.0"?>
<preset>
    <title>This is a title</title>
    <description><p>This is a paragraph</p><p>This is another paragraph</p></description>
</preset>

While the "title" is filled out correctly the "description" will return nothing. Everything after this line of code won't be executed as well.

I tried to get the content without using .html(), something like using .children() or .first(), but wasn't able to get the content from there without stripping the HTML-tags from it.

Tom
  • 188
  • 8
  • When you say it's not working on iOS, is it working on other devices? Is there any reason you're not using `.text()` like you do for title? – Clark May 27 '15 at 18:08
  • It works fine on my PC in FireFox and Chrome. It is not working on iPad and iPhone, tested with Safari, Chrome and Opera. The description node contains HTML-Tags, .text() will strip these Tags. – Tom May 27 '15 at 18:13
  • That's odd. From the JQeury docs for `.html()`, http://api.jquery.com/html/ "This method is not available on XML documents." – Clark May 27 '15 at 18:18
  • 1
    Upon considering that, this may be what you're looking for http://stackoverflow.com/questions/4980786/getting-html-from-xml-with-javascript-jquery – Clark May 27 '15 at 18:20
  • Changed it a bit and works fine! Thanks for your hint. var description = $('
    ').append($(xml).find('description').children()).html();
    – Tom May 27 '15 at 19:11
  • That's great, I'll put it in an answer so you can mark it. – Clark May 27 '15 at 19:17

1 Answers1

1

The problem is you shouldn't be able to use .html() on an XML document. A workaround can be found here Getting HTML from XML with JavaScript/jQuery

Community
  • 1
  • 1
Clark
  • 616
  • 4
  • 7