convert html object to string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mygce
    New Member
    • Jan 2011
    • 5

    convert html object to string

    I wonder how can I convert my html object to string.

    I used something like this (using mootools) but it doesn't work.
    Code:
    $('formName').toQueryString()
    I tried to alert() this but no result.

    Any idea?
    Last edited by gits; Jan 13 '11, 01:34 PM. Reason: added code tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    what type of string do you expect?

    every object has a .toString() method, though it does not always return what you want.

    Comment

    • mygce
      New Member
      • Jan 2011
      • 5

      #3
      I need to pass the elements of my form (html) on a POST...

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        you cannot simply convert an object to string. in FF you might use something like:

        Code:
        var a = {
            foo: 'foo',
            bar: 'bar'
        };
        
        alert(a.toSource());
        but it will show you an empty object-literal for dom-nodes. you might create a function that creates a string like:

        Code:
        var node = document.getElementsByTagName('body')[0];
        
        function nodeToString(n) {
            var ret = '{\n';
        
            for (var i in n) {
                ret += '\t' + i + ' -> ' + n[i] + '\n';
            }
            
            return ret + '}';
        }
        
        console.log(nodeToString(node));
        this will show you the shallow obj-structure - means the first property level ... you might recurse here but be aware of not trying to make a complete cycle - dom-nodes contain references to parent-nodes which itself contains references to the child-nodes so that with those circular references it could result in an endless recursion.

        if you just want the HTML as string then use innerHTML of the node or similar properties.

        kind regards
        Last edited by gits; Jan 11 '11, 01:20 PM.

        Comment

        • mygce
          New Member
          • Jan 2011
          • 5

          #5
          I'm not so sure because I'm using mootools 1.1 before and $('formName').t oQueryString() is working fine. But when I use mootools 1.3, it doesn't work anymore.. :(

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            what output do you expect?

            Comment

            • mygce
              New Member
              • Jan 2011
              • 5

              #7
              Something like:

              pid_638[]=587&option=com _jsubscription& controller=subs cription&task=t oggle.auto&task var=704_0

              These are element names and values of my form. I want to have them as a URL or REQUEST.

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                so you just want to extract the form's elements and it's values? are the elements of the same type like 'input'?

                Comment

                • mygce
                  New Member
                  • Jan 2011
                  • 5

                  #9
                  Thank you for the help but I guess my problem is just on mootools conflict.

                  $('formName').t oQueryString()

                  This works fine now after I resolved the conflict. There's 2 version of mootools running on my page v1.1 and v1.3 that's why it doesn't work.

                  It's working now... Thanks for your time (^_~)

                  Comment

                  Working...