Re: can't retrieve content from <span> inside a <th>



On Dec 3, 11:28 pm, Thomas 'PointedEars' Lahn <PointedE...@xxxxxx>
wrote:
lcplben wrote:
The problem page ishttp://sellmycalls.com/no-hdrtext.htmland the
problem is at lines 111-137.

The function getColHdrText (line 124) wants to retrieve the column-
header text from the second of three <span>s in the <th>. I thought
that if I only got to the <span>, I could easily access the innerHTML
just as slick as slick. It should be a five-minute task at most, but
I've been working on it for a couple of hours now with no joy.

What am I doing wrong, please?

The relevant code is (you want to post this next time):

/* Line 69 below */
function getElementsByClass ( theClass, node, tag ) {
        var classElements = new Array();
        if ( node == null )
                node = document;
        if ( tag == null )
                tag = '*';
        elems = node.getElementsByTagName( tag );
        var elemsLen = elems.length;
        var pattern = new RegExp("(^|\\s)"+theClass+"(\\s|$)");
        for (i = 0, j = 0; i < elemsLen; i++) {
                if ( pattern.test(elems[i].className) ) {
                        classElements[j] = elems[i];
                        j++;
                }
        }
        return classElements;

}

/* Line 111 below */
/* from this <th>, I want to retrieve the word "Call" from the second
<span>,
   the <span class='chtxt'...>Call</span>

<th class="cal_option_symbol" style="color:rgb(204, 0, 51);">
<span class='chimg'><img src='http://www.sellmycalls.com/pics/blu-mins.jpg'
alt='hide column' title='hide column'
     onclick="return columnHideShowByImg(this,'Call');"></span>
<span class='chtxt' title="symbol of this call option"
style="font-weight:bold;"
     onClick="return selectColumnAsFilter(this);">Call</span>
<span class='chtri'><img src='http://www.sellmycalls.com/pics/asc.jpg'
alt='ascending sort' title='ascending sort'
     onclick="return columnToggleSortOrder(this);"></span>
</th>
*/
// enter here with 'col' = the <th> element
function getColHdrText( col ) {
  var ch, s;
  ch = getElementsByClass( 'chtxt', col, 'SPAN'  );

// Firebug tells me here that ch == the 'chtxt' element, so
// I figure I should be able to just retrieve innerHTML from
// that element, right?

  s = ch.innerHTML;     // but none of these work so I'm
  s = ch.innerText;     //   missing something crucial.
  s = ch.textContent;   // what is that something?
  s = ch.value;
  return (s && s != 'undefined') ? s : 'nfg' ;

}

If you have a closer look at the getElementsByClass() function that you are
calling, starting with its name and going on with the loop inside, you can
see that it returns an array of element object references.  Therefore, you
are accessing the Array instance instead of the element object.  That Array
instance has neither property that you tried to read from.  

Firebug indicates that `ch' stores an Array instance reference by displaying
brackets in the console.  You must have missed that.  You could access the
first element of the array with `ch[0]'.

BTW, the getElementsByClass() function here is error-prone for several
reasons; search the archives for further information and better
alternatives.


Biggest problems I see are the undeclared variables (likely declared
elsewhere).

function getElementsByClass (theClass, node, tag) {
var classElements = [];
if (!node) {
node = window.document;
}
if (!tag) {
tag = '*';
}
var elems = node.getElementsByTagName(tag);
var pattern = new RegExp("(^|\\s)" + theClass + "(\\s|$)");
for (var i = 0, j = 0, l = elems.length; i < l; i++) {
if (pattern.test(elems[i].className)) {
classElements[j++] = elems[i];
}
}
return classElements;
}
.



Relevant Pages


... that if I only got to the <span>, I could easily access the innerHTML ... function getElementsByClass (theClass, node, tag) { ... are accessing the Array instance instead of the element object. ...
(comp.lang.javascript)
  • Re: Address of an array = address of its 1st element: undecidable question ?
    ... Lisp compilers, especially Lisp compilers for Lisp-machines, tend ... For instance, if there are exactly two tag bits available, we might ... and a two dimensional array is not *just* a bigger block ... with the aid of an "array descriptor" and possibly even some ...
    (comp.lang.c)
  • Re: Screen tips on without Hyperlink?
    ... just use a <span>, for example: ... note that within the first tag I've added title="Text ... visitor points their cursor at your word or phrase. ... border-bottom-width: 1px; ...
    (microsoft.public.frontpage.client)
  • Re: Seg Dump when passing variables/array between subroutines
    ... do all the reading in the one subroutine but this is a bit messy ... NEL=466 and mykeywords is a local array and keywords is the ... INTEGER:: SPAN, i ... readtemparray ...
    (comp.lang.fortran)
  • Re: Compare and delete element from an array
    ... I try to compare an arroy of Tag and an array of string. ...
    (comp.lang.ruby)