views:

87

answers:

1

The below JS function does Ajax request and retrieves HTML in obj.responseText. My issue is that I need to extract the value of id inside the span into notify_id var. I just don't know how to get that done.

This is the HTML to lookup:

HTML:

<span id="1034"></span><img src="./images/icons/post_icon.png">

JS:

function func()
{
    obj = new XMLHttpRequest();
    obj.onreadystatechange = function() {
        if(obj.readyState == 4)
            jQuery.jGrowl(obj.responseText, { 
                sticky:true,
                close: function(e,m) {
                    notifyClosed(notify_id);

                }
            });
    }
    obj.open("GET", "notifications.php?n=1", true);
    obj.send(null);
}
A: 

Since you're already using jQuery:

var responseText = '<span id="1034"></span><img src="./images/icons/post_icon.png">';
var spanId = $('<div>').html(responseText).find('span').attr('id');
alert(spanId); // 1034

The whole function in turn can also be rewritten as follows:

$.get('notifications.php?n=1', function(responseText) {
    // Your code here.
});

See also the jQuery tutorials.

BalusC
jQuery.html is not a function$.html is not a function
clonex1
Then `jQuery.growl` should have given the same problem. You need to load jQuery *before* this piece of Java code. Loading it is simple, just add the following line to HTML head, *before* any other scripts which depends on it: `<script src="http://code.jquery.com/jquery-latest.min.js"></script>`.
BalusC
Worked, thanks BalusC
clonex1
You're welcome. Don't forget to mark the answer accepted. Also see http://stackoverflow.com/faq.
BalusC
about the jquery.html is not a function error, it worked as jQuery('<div>').html(html)xxxxxxx;instead of jQuery.html(html)xxxxx;
clonex1
I didn't answered that. You apparently changed my answer in a wrong way. The `$()` is an alias for `jQuery()`, it's not the `$('<div>')`.
BalusC