0

What is the good way to parse HTML with jQuery?

I have some html like..

<ul><li>Yadayada</li><li>Yadayada</li><li>Yadayada</li></ul>

..where I would like to parse those ul and li elements away. Do I use find method and then just .remove('ul') or something like that?

And yes I am quite new with jQuery/javascript so sorry about it :)

EDIT: I want to parse away only the ul and li elements. I still want too keep the text that is insde of the ul (Yadayadas in this case).

4
  • 1
    Are you trying to take the <ul> and <li> out of your page or are you trying to extract the data contained therein? Commented Nov 8, 2010 at 20:57
  • Depends on what you want to "find" - is it text? ("Yadayada", e.g.) Commented Nov 8, 2010 at 20:57
  • Well, yes kind of I want the text yaydayada. @aaron Yes, out of my page Commented Nov 8, 2010 at 21:21
  • Answer below, haha there's a lot! Commented Nov 8, 2010 at 21:28

7 Answers 7

1

Something like this, perhaps?

$('ul').remove();

...will remove all ul elements and everything inside them.

http://api.jquery.com/remove/

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

2 Comments

Yes put I dont want to remove the content/text that is inside of that ul element.
@andrew: Could you clarify what you're trying to do? Maybe a "before" and "after" example of your HTML?
1

The best way to do so is to introduce id and class tags so you can manipulate the dom using jQuery selectors.

<ul id="my_list">

in jQuery you can get the list of objects:

$("#my_list")

You can then traverse through the list using any jQuery or Javascript traversal mechanism that makes sense for your use case.

Comments

0

Rather than "parsing" the html, you may want to work directly with the dom.

If you have a string of html to begin with, you can create a temporary dom node, and assign the html to that:

var tmp = document.createElement("DIV"); 
tmp.innerHTML = myHtml;

Then you can iterate the dom tree and do stuff to it:

var result = doStuff(tmp); 

where "doStuff()" is defined as such (note the recursion):

function doStuff (elem) {
var string = '';

switch (elem.nodeType) {
    case 1: // ELEMENT_NODE
        for (var i=0; i<elem.childNodes.length; i++)
           string += doStuff(elem.childNodes[i]);

        break;
    case 3: //TEXT_NODE
        string += elem.nodeValue;
        break;
    }
return string;
}

You can do whatever you need within there....possibly adding spaces and such between the separate strings.

This is easy to build on, and not specific to jquery....just pure javascript Dom-walking stuff. Might seem a bit verbose for a simple problem like yours, but the technique is powerful and is easy to see what is going on.

Comments

0

There are 2 types of selectors in JQuery with witch you can select any html element in the html page. Those are id selector $("#id_of_element") witch is more or less like document.getelementbyid in javascript and there are class selectors $(".cssclasname") witch are similar to document.getelementsbyname().
When you select element with some of these selectors you can call various methods on it that JQuery library contains. In your case if you want to remove an <ul> element you can add id or css class to it and use it like parameter in selector, or you can just type $('ul').remove();.
here is an example
<ul id="ul_id" class="ul_class">.....</ul>
now you can use this $('#ul_id').remove() or this $('.ul_class').remove().

Comments

0

Use replaceWith to replace the ul with the text inside it:

$('ul').replaceWith(function() {
  return $(this).text();
});

Example on JSBin

Comments

0

You can select the Elements with $("ul") and $("li"). You could then call the remove() function to remove the whole element, with everything inside. If you want to extract the Text and just get rid of the tags, you can use the text() function:

$("<ul><li>Yadayada</li><li>Yadayada</li><li>Yadayada</li></ul>").text()

returns

YadayadaYadayadaYadayada

1 Comment

I think this will do what I am looking for. Have to test it. Thanks!
0

Hmm, how about

$('li').each(function() {
  var ul = $(this).parent('ul');
  ul.after($(this).text() + '<br />');
});
$('ul').remove();

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.