www.webdeveloper.com
Results 1 to 4 of 4

Thread: frankmoore Converting a jQuery object back to a JavaScript String

  1. #1
    Join Date
    May 2010
    Posts
    6

    frankmoore Converting a jQuery object back to a JavaScript String

    jQuery noob here, so I apologize in advance if this question has been asked before.

    My situation is this: I have a string of HTML that I've extracted from my document and passed into jQuery to manipulate as such:

    Code:
    var pageEl = document.getElementById("ElementId");
    var contentToProcess = pageEl.innerHTML;
    
    // pre-process contentToProcess...
    
    var jQueryContent = $(contentToProcess);
    
    // process elements using jQuery methods
    Now, I've completed the processing that I wanted to do using jQuery and I need to extract the processed content from the jQueryContent object. How do I do this?

    There was a helpful post on stackoverflow, but the solution that they outlined isn't working. Does anyone else have any experience with this?

    Thanks.

  2. #2
    Join Date
    Feb 2010
    Location
    Amsterdam
    Posts
    174
    if you going to use jquery you can use it all the way so instead of doing

    var pageEl = document.getElementById("ElementId");
    var contentToProcess = pageEl.innerHTML;

    you can do

    var contentToProcess = $('#ElementId').html();

    example how you can do what you want if ii understand you correctly

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
      <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(function(){
    var content = $('<div><p>one</p><p>two</p></div>');
    // do some operations
    content.find('p:first-child').remove();
    // create a container to append your conmtent
    var container =  $('<div/>');
    //append the content to the container
    container.append(content);
    // if you dont append the content to a container you do not get the parent tag (<div></div>)
    alert(container.html());
    });
    </script>
        <title>Test</title>
      </head>
      <body>
        <p>Test</p>
      </body>
    </html>

  3. #3
    Join Date
    May 2010
    Posts
    6
    Gozzy, thanks but I already tried that approach. Unfortunately, the .html() method only returns the innerHTML of the object which, in Firefox, does not reflect changes which have been made using JavaScript.

    I think I have a work-around to my a problem, however, it involves going back to DOM methods and removing jQuery entirely.

  4. #4
    Join Date
    Jul 2010
    Posts
    1
    Use .get(0) instead of .html()

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles