Link to home
Start Free TrialLog in
Avatar of wjdashwood
wjdashwood

asked on

Retrieve ‘name’ from ‘textarea’ or ‘input’ html tag using IHTMLElement interface

Does anyone know if it’s possible to retrieve the ‘name’ attribute from a ‘textarea’ or ‘input’ html tag using the IHTMLElement interface or do I have to use IHTMLTextAreaElement / IHTMLInputElement interfaces? Using IHTMLTextAreaElement isn’t such an issue but as the IHTMLInputElement interface requires IE 5.0 I was hoping I might be able to avoid it as some of my users may still be on IE 4.0.

Any kind of work around would be welcome. To give some background, my program searches sequentially through a page and grabs the inner text and the name of any ‘textarea’ or ‘input’ elements on the page.

Any help would be much appreciated.
Thanks!
SOLUTION
Avatar of williamcampbell
williamcampbell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanRollins
Not all <input> or <textarea> tags have a name or ID.  

What you should do is cycle through all of the elements by getting the IHTMLElementCollection of the document (or maybe just document.all) and working through each item.  Each item will be an IHTMLElement and you can use the .tagName to see if it is an input or textarea.

-- Dan
Avatar of wjdashwood
wjdashwood

ASKER

Thanks for the comments. Firstly, I've tried using get_innerHTML although the entire body doesn't seem to fit in the BSTR. It's not an ideal solution but not a bad idea :)

I also tried implementing it inside a loop and evaluated each element to find an input or textarea tag. Is there a way of retireving the whole tag and contents as a string from the IHTMLElement? get_innerHTML only gets the text from inside the textarea tag. If it's any help, my code is shown below.


IHTMLDocument2 *pHtmlDoc = NULL;
if (SUCCEEDED(GetDHtmlDocument(&pHtmlDoc)) && (pHtmlDoc != NULL))
{
      IHTMLElementCollection* pColl = NULL;
      if (SUCCEEDED(pHtmlDoc->get_all(&pColl)) && (pColl != NULL))
      {
            long nLength = 0;
            pColl->get_length(&nLength);
            for (long nCount = 0; nCount < nLength; nCount++)
            {
                  COleVariant vIdx(nCount, VT_I4);
                  IDispatch* pElemDispatch = NULL;
                  IHTMLElement * pElem = NULL;
                  if (SUCCEEDED(pColl->item(vIdx, vIdx, &pElemDispatch))
                        && (pElemDispatch != NULL))
                  {
                        if (SUCCEEDED(pElemDispatch->QueryInterface(IID_IHTMLElement,
                              (void**)&pElem)) && (pElem != NULL))
                        {
                              BSTR bstrTagName;
                              CString sTempTagName;
                              if (!FAILED(pElem->get_tagName(&bstrTagName)))
                              {
                                    sTempTagName = bstrTagName;
                                    sTempTagName.MakeLower();
                                    SysFreeString(bstrTagName);
                              }
                              if ((sTempTagName == _T("input")) || (sTempTagName == _T("textarea")))
                              {
                                    BSTR bstrText;
                                    pElem->get_innerHTML(&bstrText);
                                    SysFreeString(bstrText);
                              }
                              pElem->Release();
                        }
                        pElemDispatch->Release();
                  }
            }
            pColl->Release();
      }
      pHtmlDoc->Release();
}


Secondly, Dan, I'm writing the HTML for the program so all <input> or <textarea> tags must have a name. I only use ID's if I need to detect a click on that item. As shown above I work through each item but how do I find the name when I have an input or textarea tag in my grasp?!

Many thanks!
I've come across getAttribute (don't know why I didn't notice it earlier) which should be exactly what I want but I'm having trouble using it. The VARIANT data type is not something I'm used to so perhaps I'm using it wrong. I had to include "comutil.h" which I assume is that correct header file.

Any ideas why varInputValue.bstrVal is NULL (0xcccccccc)?

VARIANT varInputName;
if (SUCCEEDED(pElem->getAttribute((BSTR)(_T("name")),0,&varInputName)) && varInputName.bstrVal)
{
      VARIANT varInputValue;
      if (SUCCEEDED(pElem->getAttribute((BSTR)(_T("value")),0,&varInputValue)) && varInputValue.bstrVal)
      {
            bstrText = varInputValue.bstrVal;
      }
}

The source code I found used _variant_t and _bstr_t instead of VARIANT and (BSTR) but they gave me the following compile error:

LNK2019: unresolved external symbol "void __stdcall _com_issue_error(long)" (?_com_issue_error@@YGXJ@Z) referenced in function "public: __thiscall _bstr_t::_bstr_t(char const *)" (??0_bstr_t@@QAE@PBD@Z)

Thanks once again for your help.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cheers for the code Dan. If I put it directly in my SaveUserInput function it’s not a happy bunny! I guess because I’m using CDHtmlDialog and not hosting a WebBrowser control. Is it possible to stick with what I have? I’m sure the code I already have will work with a bit more tweaking.

Do I have to initialise the VARIANT variable or include a difference header file?

Thanks.
You should be able to make just one change... to get the pointer to the document (I don't know HTML Dialogs at all, but there is *always* a way to get that).

My guess is that it will involve a call to
    GetDHtmlDocument()

>>Do I have to initialise the VARIANT variable or include a difference header file?

You should just use
        _variant_t
and
        _bstr_t
objects.  They simplify all access to these COM datatypes; for instance, they free the associated memory when they destruct at the end of the scope.

-- Dan
Thanks but I think I've cracked it! It was all to do with:

pElem->getAttribute((BSTR)(_T("name")),0,&varInputName)

which should have been:

pElem->getAttribute(L"name",0,&varInputName)

Just what do the L and the _T do anyway? Just out of interest, why did I get unresolved external symbol errors when I used _variant_t and _bstr_t instead of VARIANT and BSTR?

Many thanks for you time and effort. Well worth some points anyway.
I don't know why you would get about unresolved externals.  The headers are in
    #include <comdef.h>
which also pulls in several libraries...
    #pragma comment(lib, "comsupp.lib")
    #pragma comment(lib, "user32.lib")
    #pragma comment(lib, "ole32.lib")
    #pragma comment(lib, "oleaut32.lib")
They may have changed something in the MFC 7 (aka VC++.Net)

>>Just what do the L and the _T do anyway?
L"xxxx" forces the compiler to store the string literal internally as UNICODE (16-bit wide) characters.  All COM interfaces expect 16-bit wide characters.

_T("xxxxx") forces the compiler to store the string literal as either UNICODE or 8-bit characters, depending upon whether you have defined UNICODE or not in your preprocessor settings.

Given that
        pElem->getAttribute((BSTR)(_T("name")),0,&varInputName)
failed but
        pElem->getAttribute(L"name",0,&varInputName)
worked, I'd say that you are working with a non-UNICODE build.

-- Dan
Ah I see. Many thanks for the help! Much appreciated. Night night.