5

I have a variable that contains simple HTML looking like the following example - the content of the variable is generated dynamically via Ajax:

var errors = "<span id='id1'>Value 1</span><span id='id2'>Value 2</span><span id='id3'>Value 3</span><span id='id4'>Value 4</span><span id='id5'>Value 5</span>";

How can I get the text of a specific element by its ID within this variable, e.g. the text of span with ID='id3' ? Here the result should be "Value 3".

I tried the following but the result I get is always either an empty string or "[object Object]":

  1. $(errors).filter('#id3')
  2. $(errors).filter('#id3').text()
  3. $(errors).find('#id3').text()

Update:
After reading through the comments and answers on this I split my code up and it seems the issue is with the success part of the Ajax where data contains what I showed in the example above but it seems it is not getting stored in the variable "errors". (If I hardcode the variable then it works with .filter .)

var errors = '';
$.ajax({        
    type: "post",   
    url: "ajax.php",
    cache: "false",
    data: {
        node: 'fetchErrors',
        selectedLang: selectedLang
    },
    success: function(data){
        errors = data;
    }
});

Can someone help me with this ?

Many thanks in advance, Mike

keewee279
  • 1,656
  • 5
  • 28
  • 60
  • 3
    I tried your 2nd ans ($(errors).filter('#id3').text()) in firefox working fine. –  Jul 04 '15 at 09:06
  • 1
    concur with @JohnDaniel ... working fine in chrome. – manta Jul 04 '15 at 09:11
  • @JohnDaniel: Thanks for this. I can confirm this now as well and just made an update to my post. – keewee279 Jul 04 '15 at 09:12
  • @manta: Thanks as well. Just updated the post. – keewee279 Jul 04 '15 at 09:15
  • @keewee279, then write this line console.log(typeof data); and see the type returned from server. If it is not string then convert it. –  Jul 04 '15 at 09:19
  • 1
    @keewee279 you may have async problems. Append `async: false` to your ajax object. – manta Jul 04 '15 at 09:20
  • @JohnDaniel: Thanks - the type is string already. – keewee279 Jul 04 '15 at 09:21
  • @manta: Thanks ! Can you explain what this does ? – keewee279 Jul 04 '15 at 09:22
  • 1
    @keewee279, you are doing filter before the success function hits. –  Jul 04 '15 at 09:23
  • I am just wondering if my jQuery is "running too fast" so that it already moves on before it actually has the result from the Ajax since everything else happens outside the Ajax. – keewee279 Jul 04 '15 at 09:24
  • You are right: check this link for details on `async : false` http://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax try to execute your code within the `success` function – manta Jul 04 '15 at 09:24
  • Update: Thanks all ! Just accepted an answer on this. My issue was indeed with the Ajax since jQuery moved on before having the Ajax result so moving everything inside the success part fixed it and then the approach with .filter works fine - the comments pointed me in the right direction so thanks for them ! – keewee279 Jul 04 '15 at 09:28

4 Answers4

4

You were on the right track...

var errors = "<span id='id1'>Value 1</span><span id='id2'>Value 2</span><span id='id3'>Value 3</span><span id='id4'>Value 4</span><span id='id5'>Value 5</span>";
var obj = $(errors);
var filter = $(obj).filter('#id3'); 
console.log(filter.text());

jsfiddle: fiddle

manta
  • 1,663
  • 12
  • 12
2

try to add async: false to ajax properties.

2

This works for me (fiddle).

success: function(data){
    var result = $(data).filter("#id3").text();
}
Daniel Dušek
  • 13,683
  • 5
  • 36
  • 51
  • Thanks. I am going to accept this one since it pointed me in the right direction. My issue, however, was with the Ajax since jQuery moved on before having the Ajax result so moving everything inside the success part fixed it and then this approach works fine. – keewee279 Jul 04 '15 at 09:27
  • @keewee yes ajax is ```asynchronous``` which means the function finishes before the results (```data``` in your case) come from server. – Daniel Dušek Jul 04 '15 at 09:34
1

This does the job:

$(errors).filter("#id3").text()
Antonio Smoljan
  • 2,187
  • 1
  • 12
  • 11