Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jQuery Object to HTML?

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #40045
    jacorre
    Participant

    Hi everyone,

    I’m finding divs by class name which creates an object. I want the html of that object so I can append it to the body of the page. Can that be done?

    So for example:


    // The html...
    <div class="findme">Hello</div>
    <div class="findme">Hello again</div>

    // The jquery...
    obj = $('body').find('.findme')

    So obj would result in an object. How can I output that object as html?

    #110942
    TheDoc
    Member

    You could just do:

    var obj = $(‘.findme’).html();
    $(document.body).append(obj);

    #110943
    SgtLegend
    Member

    You can simply use the append methods jQuery comes with.

    $(‘.findme’).appendTo(‘body’);

    #110945
    jacorre
    Participant

    Might not be explaining myself good.

    After finding all the divs with the class I’m looking for, I end up with those divs wrapped up as an object. So when I try and append to the body all I get is Object Object.

    What I want is all the HTML in that object.

    #110947
    jacorre
    Participant

    Will have to try .html() again because I had already tried it but didn’t think it worked as expected.

    #110952
    TheDoc
    Member

    I’m really not sure what you’re trying to do, but this should get you closer: http://codepen.io/ggilmore/pen/GkrdE

    #110961
    SgtLegend
    Member

    If your building the HTML elements with jQuery then you can still use the above code in my previous post but instead pass in the object and jQuery will know what to do with it.

    var myObj = $(‘

    ‘);
    myObj.appendTo(‘body’);

    Ignore the black slash in the code, for some reason its been treated as HTML which means the markdown handler is broken on the forum.

    #110970
    jacorre
    Participant

    .html() only seems to work on an element that contains the divs I want to grab. I don’t have a containing element except body and I don’t want to grab ALL of the html in body.

    So I use find to grab all the divs with a class of findme. That results in one object with the divs inside.

    What I’m really looking to do with that is save it to local storage. Then when the page loads again, load what’s in local storage, which needs to be all the html, not an object.

    So my problem is that when I append to the body, the result I’m getting is Object, not the actual html.

    I’m wondering if I have to convert the object to a string?

    #110972
    jacorre
    Participant

    I might have to change my approach actually now that I’ve been testing some more. I think I need to have a containing element and it might actually be easier. Thanks for everyone’s help!

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.