#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0

    Newbie Javascript/CSS question


    Hi everyone,

    Here is the problem I am facing, hope someone can help.

    I have written some javascript that allows the contents of a div element to change, when a link is clicked. However, I cannot get any formatting to happen to that text, more specificaly I want to display a link, and it does not get rendered as such, rather as text. Is this the default behaviour and is there a workaround for this?

    Thank you!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Posts
    1,882
    Rep Power
    19
    could you post your code
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0
    Here you go,

    ==HTML Snippet==
    <div align="left" class="myMainText" id="mainText">&nbsp;</div>

    This is the div element that gets populated with text when clicking on a url referring to the javascript code

    ==HTML Snippet==

    ==JavaScript Snippet==
    function getText(mainTextId) {
    var mainText = ""
    switch (mainTextId) {
    case "whatis"
    mainText = "<a href='somelink.html'>test</a>"
    break;
    }
    return mainText
    }
    function unhide(mainTextId) {
    document.getElementById('mainText').firstChild.nodeValue=getText(mainTextId);
    }

    Instead of the "<a href='somelink.html'>test</a>" being translated into a link it is displayed as-is
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2001
    Posts
    765
    Rep Power
    13
    You're writing text to a text node which is displaying it as...erm, text. Might want to re-think this - here's a possibility:

    <html>
    <head>
    <script type="text/javascript" language="javascript">

    function set_link(mainTextId) {
    var newlink = document.createElement('a');
    switch (mainTextId) {
    case 'whatis' :
    newlink.setAttribute('href', 'somelink.html');
    var txt = document.createTextNode('test');
    newlink.appendChild(txt);
    break;
    }
    var oldchild = document.getElementById(mainTextId).firstChild;
    document.getElementById(mainTextId).replaceChild(newlink, oldchild);
    }

    </script>
    </head>
    <body>
    <a href="#" onclick="set_link('whatis');return false;">GO</a><br /><br />
    <div align="left" class="myMainText" id="whatis">....</div>
    </body>
    </html>

    http://www.brainjar.com/dhtml/intro/default.asp
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0
    I'll do that, cheers .

IMN logo majestic logo threadwatch logo seochat tools logo