jquery - Unable to retrieve element by id from an HTML string -


i have php returns html snippet string in following format:

echo '<tr><td>blah</td><td>more blah</td></tr><tr><td>blah</td><td>more blah</td></tr><tr><td>blah</td><td>more blah</td></tr><span id="morerows">1</span>'; 

now @ client, using jquery (2.1.4) extract text of #morerows (in example, 1) local var ifmore , remove <span> original html before further processing. following trying testing purposes:

var hr = createxmlhttprequestobject(); ... var return_data = hr.responsetext; var rdashtml = $(return_data); var ifmore = $('span#morerows', rdashtml); alert(ifmore.text()); 

but alerts blank. http request processing fine because alert(return_data); shows value expected. extraction of <span> element somehow not working. there missing out?

you have wrap code in div, because jquery parsing first tag , ignoring rest. code should be:

echo '<div><tr><td>blah</td><td>more blah</td></tr><tr><td>blah</td><td>more blah</td></tr><tr><td>blah</td><td>more blah</td></tr><span id="morerows">1</span></div>'; 

to explain further:

console.log($("<tr><td>test</td></tr><span>test</span>")[0].outerhtml); 

gives:

"<tr><td>test</td></tr>" 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -