views:

67

answers:

4

I make a $.get call to my db and it returns some HTML (data):

$.get(url, null, function(data) {

The HTML it returns is something like this:

<div id="1234" class="myclass">..more html..</div>

In my callback function, I try to add a class to it (based on some conditionals that I have tested are successfully being reached) like so:

if (someCondition) $(data).addClass('mynewclass' + someId);

Even when someCondition is true, my HTML is not having the mynewclass[someId] class added to it. Is there any reason why this may be that I'm just stupidly overlooking?

Thanks... :\

EDIT

Here's a link to a reproducible example. It's not EXACTLY what i'm doing per se (i'm not using a var outside to get my data, but i'm reproducing the same effect). notice how it's showing 'false' for the test.

+3  A: 

jQuery like raw javascript works on DOM. And what you are doing, is trying to manipulate on data( HTML as long string in your case). You need to atleast add HTML data you requested to DOM, before you start doing tricks with it. Your callback should be something like this..

function(data){
  $('#container_to_fit_data').html(data); // Adding to Document
  someId = 1234;
  if (someCondition) 
      $('#'+someId, '#container_to_fit_data').addClass('mynewclass' + someId);
      // or lets see it in simplest form
      // $('#container_to_fit_data').find('#'+someId).addClass('mynewclass' + someId);
}

[ EDIT ]

@Anurag Its true that jQuery is able to manipulate strings data, but its not true that its the case with non-ID transactions.

You can see,

DOM_STR1 = "<div>
              <p>
                 <strong>Strong</strong>
                 <span class='myclass'>Span</span>
              </p>
            </div>"

DOM_STR2 = "<p>
              <strong>Strong</strong>
              <span class='myclass'>Span</span>
            </p>"


$('span.myclass', DOM_STR1) // We find span. Allright.
$('span.myclass', DOM_STR2) // We find span. Cool.

$('p', DOM_STR1) // We find p too. But..
$('p', DOM_STR2) // Empty. Never return p, Why??
$('div', DOM_STR1) // Empty Again. Why??

'div' is present in DOM_STR1, same way 'p' is present in DOM_STR2. Why jQuery can't read the wrapping element, but finds children from string??

So, when data is anyway needed to be on DOM, it makes no sense to manipulate it from string.

simplyharsh
i will try this... thx
Jason
That's not true. Only when dealing with id's, do we need to insert into the DOM. Otherwise, we can directly create and manipulate elements outside of the document perfectly fine - `$('<p>').addClass('someClass');`
Anurag
@simplyharsh i posted a link to some code
Jason
@simplyharsh re:your edit - if you simply call `$(DOM_STR1)` instead of `$('div', DOM_STR1)` you have access to the outer layer. my problem was that i was making changes to the object but not permanently. once i set it to a local var and made changes to that, everything was gravy
Jason
+1  A: 

@Jason: thats my point, you need to use .addClass on a jQuery object, not raw HTML. Or you need to pattern match 'data', treating it as a string (which it is) and inject your class into the class attribute.

Moin Zaman
@moin when you wrap it in `$()` it becomes a jquery object
Jason
OP *is* using `.addClass()` on a jQuery object. `$(data)` is a jQuery object with the HTML string passed in and rendered into DOM elements. A jQuery object can accept raw HTML.
patrick dw
A: 

The "data" variable refers to the entire collection of returned information. Something like this should work:

if(someCondition) { $(data).find('#1234').addClass('mynewclass'+someId); }
RussellUresti
i thought of that, but doing that doesn't work. i tried a $(data).is('[a class already on it]') and it returns true, so i'm actually at the top of the tree
Jason
@simplyharsh yes it will. `$(data)` will turn it into a valid jquery object that you can manipulate. the trick is to set it to a local var so that the changes stick :)
Jason
+3  A: 

The problem is you are creating multiple div objects. The first one you are creating is being discarded. Then a new one is created which does not have the class that was previously added.

Hold on to a reference of the div that was first created and to which the class was applied. Then add that same div to the page.

function(something) {
    var div = $(data).addClass('someClass');
    $('#container').html(div + div.is('.someClass'));
}

However, now we are dealing with an object (div) and not a string, and trying to convert an object to a string will yield "[object Object]". So modify the append function to this:

$('#container').empty();
$('#container').append(div).append(String(div.is('.someClass')));

See your example updated.

Anurag
+1 You were quicker than I was, although you would need to do `div.html() + div.is('.anotherclass')` so you don't get `[object Object]true`. EDIT: Actually, I'm not quite right on that since `.html()` only gives you content. Your update is correct. :o)
patrick dw
thanks @Patrick :) I realized that after actually running the code.
Anurag
GOD I FEEL SO DUMB. thank you anurag. the part about setting my data to a local var solved the problem.
Jason
LOL we have all been there. Some of the functionality of append rears it fully functional head :)
Mark Schultheiss
@Jason - the programmers brainfart is a common thing. don't worry about it, we've all been there and sometimes practically lived there :)
Anurag