Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Traversing through HTML data?

  • 15-08-2012 02:37PM
    #1
    Registered Users, Registered Users 2 Posts: 543 ✭✭✭


    Specifically <div> tags, but there are some classes there too. It's a mess. Ideally I'd convert it to an XML and be able to access the elements via the div name or something. I'm not entirely sure what way to go about it.

    Effectively I've an AJAX call that returns HTML data (don't ask why) and I want to extract the elements from the code.


Comments

  • Registered Users, Registered Users 2 Posts: 2,062 ✭✭✭Colonel Panic


    What have you tried? I'm assuming Javascript here since you mention getting the HTML via an AJAX call. You can traverse that data with Javascript.


  • Registered Users, Registered Users 2 Posts: 3,805 ✭✭✭Setun


    Why not use jquery if you're looking for a quick solution? Should be handy enough with selectors, unless the html is really all over the place.


  • Registered Users, Registered Users 2 Posts: 46 Paddy GT


    I would agree ... JQuery is best for this - as long as your divs have id or class attributes traversing should be pretty easy


  • Registered Users, Registered Users 2 Posts: 543 ✭✭✭solarith


    I was using jQuery for some, but others do not have id's or names! Mad, eh?

    Can I use jQuery to find, for example, the first instance of a <p>, the second instance of a <p>, etc..?


  • Registered Users, Registered Users 2 Posts: 27,514 ✭✭✭✭GreeBo


    switch the call to a server side call and process it there, then return that from your Ajax call.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 543 ✭✭✭solarith


    GreeBo wrote: »
    switch the call to a server side call and process it there, then return that from your Ajax call.
    I don't have access to the server ;D

    OK - here is the code to process the 1st element without a name, for anyone who needs:

    var variable = $("a:eq(0)").html();


    So this will get the text in the 1st <a .....> </a>, increase 0 for others.


Advertisement