#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2006
    Location
    Singapore
    Posts
    77
    Rep Power
    15

    How to convert an object to String to display in alert box?


    Hi,

    I got an output [object] when I am trying to print a parameter using an alert box via the following code:

    Code:
    function validate(sID){
    alert(sID);
    
    }
    I tried alert(sID.toString()); but it didn't work out either..

    May I know how can I get the alert box to display the value?

    Thank you very much.
  2. #2
  3. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,557
    Rep Power
    604
    Originally Posted by KC Tan
    May I know how can I get the alert box to display the value?
    Whom value? An object has no value. An object has properties, and properties have values. Presuming an object like:
    Code:
    var sID={
    'hair':'blond',
    'eyes':'blue'
    }
    You may display the value of its 'hair' property:
    Code:
    function validate(sID){
    alert(sID['hair']);
    }
    Or you may loop to display all it's properties values:
    Code:
    function validate(sID){
    for(prop in sID){
    alert(sID[prop])
    }
    }
    And an object can not, of course, be converted into a string, unless some special cases, which I am sure it is not your case.

    The above notes show you what an object really means in javascript. Now, looking on your code, I suspect you wanted to say element, not object (even if an element is nothing but an HTML special object), and in fact the passed argument sID is the id of the element.

    Can you tell us what sID really is? Can you detail your code, please, HTML included?
    Last edited by KorRedDevil; June 3rd, 2008 at 04:37 AM.
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,124
    Rep Power
    4303
    toSource with arrays in IE might be helpful.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2006
    Location
    Singapore
    Posts
    77
    Rep Power
    15
    Hi, thanks for the reply. sID is merely a id of a text field. For example, UserName.

    I will work on the properties and see if it can output the value and update again..

    Thank you very much again for the replies
  8. #5
  9. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Aug 2005
    Location
    Bucharest ROMANIA
    Posts
    2,557
    Rep Power
    604
    Originally Posted by KC Tan
    Hi, thanks for the reply. sID is merely a id of a text field. For example, UserName.
    If the output is [object], that means your sID passed parameter is not the id of a textfield, but straight the text field as an object. Can we see the HTML part with the handler which call the function? You must have something like <form onsubmit="validate(something)"> or <input type="text" onchange="validate(something)">. We need to see that something. That something must be a string to be an id, not an object.

    Do we need to hardly extract the useful information from you?
    Last edited by KorRedDevil; June 4th, 2008 at 03:14 AM.
  10. #6
  11. 4:04 Time Not Found
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2004
    Location
    Northern Virginia
    Posts
    2,273
    Rep Power
    338
    I don't do any alerts anymore, I use the Firebug add-on for Firefox and their console.log() function, like:
    Code:
    console.log(sID);
    This will usually display the full details of whatever object you pass, if not, like Kravvitz suggested, use toSource():
    Code:
    console.log(sID.toSource());
    I am so smart, I am so smart, S.M.R.T ... I mean S.M.A.R.T.

    Stop Using Pop-Ups
  12. #7
  13. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,007
    Rep Power
    2791
    I'm not even going to comment.
    Javascript Code:
    alert(document.getElementById(sID).value);
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.

IMN logo majestic logo threadwatch logo seochat tools logo