1

I would like to parse some html content from a textarea and get a value from a selected tag using jquery. I tried the below code but no hope.

var a = $('#a').val();
 var dom_nodes = $($.parseHTML(a));
 alert(dom_nodes.find('#ae').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<textarea id="a">
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html b:version='2' class='v2' expr:dir='data:blog.languageDirection' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'>
 <b:if cond='data:skin.vars.body_background.image and data:features.responsiveBackgrounds' id='ae'>
      <b:with value='&quot;body&quot;' var='selector'>
        <b:include cond='not data:view.isPreview' data='skin.vars.body_background.image' name='responsiveImageStyle'/>
      </b:with>
    </b:if>
    </html>  
</textarea>

4
  • The error clearly indicates that you did not include the jQuery library. Commented Aug 13, 2017 at 18:56
  • You are missing jQuery library reference to the page. Commented Aug 13, 2017 at 18:56
  • even after including jquery I dont the inner html Commented Aug 13, 2017 at 18:59
  • That looks remarkably like XML, not HTML... the <?xml is the first clue... Commented Aug 13, 2017 at 19:57

2 Answers 2

1

Use parseXML instead.

var a = $('#a').val();
var dom_nodes = $($.parseXML(a));
alert(dom_nodes.find('#ae').html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<textarea id="a">
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html b:version='2' class='v2' expr:dir='data:blog.languageDirection' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'>
 <b:if cond='data:skin.vars.body_background.image and data:features.responsiveBackgrounds' id='ae'>
      <b:with value='&quot;body&quot;' var='selector'>
        <b:include cond='not data:view.isPreview' data='skin.vars.body_background.image' name='responsiveImageStyle'/>
      </b:with>
    </b:if>
    </html>  
</textarea>

Sign up to request clarification or add additional context in comments.

8 Comments

for this sample code..the above solution works fine...but I'm dealing with lots of code that comes under the textbox. In that case it shows none :(
and most of the tags that comes in the textarea does'nt have a end tag like xml does..
Try running it through here to see where it's messed up codebeautify.org/xmlvalidator
like I mentioned earlier, most the tags in the code does'nt have an end tag.. its self closed. So it shows invalid XML.. in that case how can I fetch data from the XML file
$.parseXML can definitely process self closed tags. Even in this snippet, b:include is self closed. Are you sure there's nothing else causing it to be invalid?
|
0

You can use DomParser to get the ae node.

var xml = document.getElementById('a').value;
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xml, "text/xml");
var ae = xmlDoc.getElementById('ae');
console.log(ae.id);
<textarea id="a">
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html b:version='2' class='v2' expr:dir='data:blog.languageDirection' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'>
 <b:if cond='data:skin.vars.body_background.image and data:features.responsiveBackgrounds' id='ae'>
      <b:with value='&quot;body&quot;' var='selector'>
        <b:include cond='not data:view.isPreview' data='skin.vars.body_background.image' name='responsiveImageStyle'/>
      </b:with>
    </b:if>
    </html>  
</textarea>

Comments

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.