sawo
sawo

Reputation: 27

get html and text data from ajax request with .find

I have the following Jquery code

     $('body').append('<div id="berichten"; style="border: 2px solid rgb(150, 0, 0); padding: 5px 7px; background: rgb(150, 0, 0) none repeat scroll 0% 0%; opacity: 0.85; position: fixed; z-index: 9000; top: 163px; text-align: left; font-family: Arial,Helvetica; font-size: 10px; -moz-border-radius: 5px; color: #FFFF66;"></div>');

     (function worker() { 
 $.get('/events', function (data) { 
    var berichten = "";
    $(data).find(".eventtitle.notread a:contains('text 4')").each(function () {
        berichten += $(this).html() + "<br />";
    });
    setTimeout(worker, 10000);
    $('#berichten').html(berichten);
      });
 })();

The HTML data from the page i get with the ajax request is

     <table>
<tr>
    <td class='eventchkbox' width="2%">
        <input type="checkbox" name="del[]" value="2351446" />
    </td>
    <td colspan="2" class='eventtitle notread'> <a name="event2351446" style="position:relative;top:-135px;left:-8000px;">&nbsp;</a><a href="/events/page/1/2351446#event2351446">text 4</a> 
    </td>
    <td class="eventdate">09-06-2013 21:12:12</td>
</tr>
<tr>
    <td class='eventchkbox' width="2%">
        <input type="checkbox" name="del[]" value="2351440" />
    </td>
    <td colspan="2" class='eventtitle '> <a name="event2351440" style="position:relative;top:-135px;left:-8000px;">&nbsp;</a><a href="/events/page/1/2351440#event2351440">Text 2</a> 
    </td>
    <td class="eventdate">09-06-2013 21:11:38</td>
</tr>
<tr>
    <td class='eventchkbox' width="2%">
        <input type="checkbox" name="del[]" value="2348501" />
    </td>
    <td colspan="2" class='eventtitle '> <a name="event2348501" style="position:relative;top:-135px;left:-8000px;">&nbsp;</a><a href="/events/page/1/2348501#event2348501">Text 3</a> 
    </td>
    <td class="eventdate">09-06-2013 19:04:35</td>
</tr>
<tr>
    <td class='eventchkbox' width="2%">
        <input type="checkbox" name="del[]" value="2348489" />
    </td>
    <td colspan="2" class='eventtitle notread'> <a name="event2348489" style="position:relative;top:-135px;left:-8000px;">&nbsp;</a><a href="/events/page/1/2348489#event2348489">text 4</a> 
    </td>
    <td class="eventdate">09-06-2013 19:04:01</td>
</tr>

The Jquery code gives the result:

text 4
text 4

I also want to get the url in the response like:

text 4
text 4

It works when i use: $(data).find(".eventtitle.notread").each(function () { I gives back than all the uread classes

but when i add a:contains('text 4') it only gives back the text string.

I created a jsfiddle for this http://jsfiddle.net/sawo/LJegm/4/

What I try to achieve with this script is that all the unread messages (on the /event page) with the class "eventtitle notread" AND the text "text4" get displayed on the first page. I want the link on the first page so i can directly access the message.

Upvotes: 0

Views: 258

Answers (1)

PSL
PSL

Reputation: 123739

You could use native DOM Element's outerHTML property instead of $(this).html(). jquery .html() will provide you only the inner contents of your anchor tag under question which in this case is just a text. So you should use outerHTML to get the actual anchor tag html string as it is.

 berichten += this.outerHTML + "<br>";

Fiddle

or you could just do in a line:

      $('.eventtitle.notread a:contains("text 4")')
      .clone()
      .css('display', 'block')
      .appendTo($('#berichten'));

Also just do remember in your fiddle there is a ; after the id in place which may make your html invalid so just remove that.

$('body').append('<div id="berichten"; style="border:

                                     ^_______________________________________Remove this

Upvotes: 1

Related Questions