Search code examples
jqueryphpquery

enumerate tags by name


I want to get tag the names from a page using jquery so I can use them as variables.

Example:

Tags:
<title>Item Name</title>
<price>$10.00</price>

$tag_name[0] = first tag (this is where jquery code would go)
$tag_name[1] = second tag (this is where jquery code would go)

$item_array[$tag_name[0]][0] = "item name";
$item_array[$tag_name[1]][0] = "$10.00";

How can I get the actual name of the tags? I know how to get the contents.


Solution

  • You can use the tagName property to get the tag name.

    However, you still need a way to select the tags. If they're contained within an element (eg: .container), you could do something like:

    $(":eq(0)", ".container")[0].tagName // title tag
    

    Demo