children["id"].innerText - does not work with netscape but with explorer

Discussion in 'Javascript' started by Julia Peterwitz, May 5, 2004.

  1. I have a function that works with explorer but not with netscape.
    The problem is the function at line 5.


    1 fSetSelectedDay(myElement){
    2 /*
    3 ...
    4 */
    5 var elementText = eval(myElement.children["calDateText"].innerText);
    6 /*
    7 ...
    8 */
    9 }
    10 <!-- ... -->
    11 <td id=calCell onclick='fSetSelectedDay(this)'>
    12 <font id='calDateText' onclick='fSetSelectedDay(this)'>
    13 <script> myMonth[w][d] </script>
    14 </font>
    15 </td>
    16 <!-- ... -->


    I need some support for this.
    Thank you very much.
     
    Julia Peterwitz, May 5, 2004
    #1
    1. Advertisements

  2. Julia Peterwitz

    Randy Webb Guest

    ..innerText is IE only, and as such is not going to work in NS. use
    innerHTML instead.

    eval is not needed there:

    var elementText =
    document.getElementById(myElement).children('calDateText').innerHTML;

    the [ ] are also an IE-ism.
     
    Randy Webb, May 5, 2004
    #2
    1. Advertisements

  3. Agreed, however...
    ....there are two problems with this line:

    1) If you look at the OP's HTML, you'll see that myElement is a
    reference to the element, not an id.
    2) The children property is
    a) a collection, so the brackets were correct,
    b) also a Microsoft-ism.

    Perhaps

    var elementText = myElement.childNodes[ 'calDateText' ].innerHTML;

    would be better (preferably with feature detection)?

    Mike
     
    Michael Winter, May 5, 2004
    #3
  4. myElement.childNodes[ 'calDateText' ].innerHTML;
    is also not working
    (it neither works in explorer nor in netscape)

    any other proposals for netscape??

    solution for explorer again:
    myElement.children["calDateText"].innerText;
     
    julia peterwitz, May 5, 2004
    #4
  5. Oops. I assumed that childNodes was a standard collection that could be
    indexed by name/id as well as ordinal. However, I just thought that
    'calDateText' is an id, so why don't you just use

    document.getElementById( 'calDateText' ).innerHTML

    ?

    Mike
     
    Michael Winter, May 5, 2004
    #5
  6. You don't say which Netscape. If it's Netscape 4, your options are
    very limited. If it's Netscape 6+, i.e., based on Mozilla, then
    you can pretty much fly :) I'll assume Netscape 6+.
    I see three problems:
    - innerText is IE only.
    - children is IE only.
    - eval is evil.

    There are different options, depending on how standards compliant you
    want to be. I'll go for full compliance with the DOM specification
    (i.e., avoiding innerHTML).

    A version that collects the string content of the calcDateText element
    (not recursively, so don't put the text to find inside tags).
    ---
    var span = document.getElementById("calcDateText");
    var elementText = "";
    for(var chld = span.firstChild; chld; chdl=chld.nextSibling) {
    if (chld.nodeType == 3) { // text node
    elementText += chld.nodeValue;
    }
    }
    ---
    Or, change the first line to:
    ---
    var span = myElement.getElementsByTagName("span")[0];
    ---
    (Notice that I use "span" instead of "font" as commented later)
    The font tag is deprecated and you are not using any of its specific
    The type attribute is required on script tags, so:
    ---
    <script type="text/javascript">
    ---
    Do you mean to document.write the value?
    ---
    document.write(myMonth[w][d]));
    ---
    Where are "w" and "d" calculated? (Meaning Week and Day?)
    </span>

    /L
     
    Lasse Reichstein Nielsen, May 5, 2004
    #6
  7. yes, I thought about netscape 6+
    until here it works.
    but the following doesn't start.
    and I don't know why.
    but there is no error message.
    I removed the irrelevant attributes.
    sorry I forgot to put the document.write around

    13 <script> document.write(myMonth[w][d]); </script>

    this function notes the days of the months, but this is irrelevant for
    the problem. isn't it?
     
    Julia Peterwitz, May 6, 2004
    #7
  8. Damn! :(
    That would be the typo: ^^^^ shoud be chld.

    That would mean that the loop goes on forever ... doing nothing.

    /L
     
    Lasse Reichstein Nielsen, May 6, 2004
    #8
  9. /*
    ....
    */

    alert("1");
    var font = myElement.getElementsByTagName("font");
    alert("2");
    var elementText = "";
    alert("3");
    for(var chld = font.firstChild; chld; chld=chld.nextSibling) {
    alert("4");
    if (chld.nodeType == 3) { // text node
    elementText += chld.nodeValue;
    }

    /*
    ....
    */

    when I start this I'll get the alert's 1,2,3 but the alert 4 doesn't start.
    so I think there is something wrong.
    but I don't know what.
     
    Julia Peterwitz, May 7, 2004
    #9

  10. elementText = document.getElementById('calDateText').innerHTML;
    alert(elementText);

    good idea, but elementText is "".
    do you know why?
     
    Julia Peterwitz, May 7, 2004
    #10
  11. Julia Peterwitz

    ZER0 Guest

    Try something like that:

    <script type="text/javascript">
    function getInnerText(el){
    return
    el.innerHTML.replace(/(<script[^>]*?>.*?<\/script>)|[\r\n]/gi,"").replace(/<[\/\!]*?[^<>]*?>/g,"");
    }

    function fSetSelectedDay(el){
    alert(">"+getInnerText(el)+"<");
    }
    </script>

    <font id='calDateText'
    onclick='fSetSelectedDay(this)'><script>document.write('some text,<br />
    maybe with <strong>tags</strong>');</script></font>

    I hope it helps.


    --
    C'ya,
    ZER0 :: coder.gfxer.webDesigner();

    Il computer e' una macchina progettata per velocizzare e automatizzare
    gli errori.
     
    ZER0, May 7, 2004
    #11
  12. [snip]
    Not a clue. Can you post a complete sample (preferably a URL)?

    Mike
     
    Michael Winter, May 7, 2004
    #12
  13. http://www.fligo.de/main/calender.js
    is the url for the script

    http://www.fligo.de/main/lastminute.php
    is the url for the page
     
    Julia Peterwitz, May 11, 2004
    #13
  14. Looks like there is a closing brace missing. Does the JavaScript
    Console tell you anything? If yes, what does it say?


    PointedEars
     
    Thomas 'PointedEars' Lahn, May 30, 2004
    #14
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.