Question: I have a string that contains an HTML code snippet and I want to get the value of the `id` attribute from the element `dt`.

```
var myHtml = "<dt id='119'>Email</dt><dd id='container'>my text</dd>";

```

Expected result:

```
var myId = 119;

```

Does anyone have any ideas how I can do this?

Answer: 
If the order of elements may be changed you should find the specific `dt` then gets its `id` attribute. `attr()`, by itself, will only return the first matching id found so is not a robust solution:

```
$(myHtml).filter('dt').attr('id')

```

You have to use `filter()`, and not `find()` as they are all top level elements in that HTML snippet.

<http://jsfiddle.net/TrueBlueAussie/ghwzsao0/>

The safer alternative, if the content can vary, is to wrap the html in another temp element that `find()` can be used with:

```
$('<div>').html(myHtml).find('dt').attr('id')

```

<http://jsfiddle.net/TrueBlueAussie/ghwzsao0/1>
You can test this in console:

```
$("<dt id='119'>Email</dt><dd id='container'>my text</dd>").eq(0).attr('id')

```