converting a [object HTMLCollection] into string in javascript

余生颓废 提交于 2019-12-08 02:55:38

问题


I am trying to use data extracted from a XML file by getElementByTagName and it returns HTML Collection Object but I need this data for a sending a REST request so I need to get the HTML Collection Object to be converted into a string. How can it be done?

Here's more information:

com_zimbra_om.prototype._responseHandler=
        function(response){
                try{
                    sid = response.xml.getElementsByTagName("session_id");
                    this.login_user();
                    }catch(e){
                            this._showErrorMsg(e);
                            }

Using this function I am trying to get the session_id from a REST response. Here sid (global variable) is the HTML Collection Object. Now when I try to use this in the next function:

com_zimbra_om.prototype.login_user = function(){
var url = selected_server + 'services/UserService/loginUser?SID=' +
                                    sid + '&username='+
                                    selected_username +
                                    '&userpass=' + 
                                    selected_password;
                var request_url = ZmZimletBase.PROXY + AjxStringUtil.urlComponentEncode(url);

So here I am using sid which I need as a string.

So how should I convert HTML Collection Object into string??

Thanks


回答1:


With this information I can only go with

var objectHTMLCollection = document.getElementsByTagName("div"),
    string = [].map.call( objectHTMLCollection, function(node){
        return node.textContent || node.innerText || "";
    }).join("");


来源:https://stackoverflow.com/questions/11125825/converting-a-object-htmlcollection-into-string-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!