Results 1 to 9 of 9

Thread: help with innerHtml

  1. #1
    Join Date
    Nov 2007
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default help with innerHtml

    hello

    for example I have this function:

    function ChangeTxt(txt) {
    document.getElementById('ref').innerHTML = txt;
    }


    and this code in the body:

    <td><a href="#nogo" onClick="ChangeTxt('ref:34<br>available sizes:s-m-l')"></a></td>


    if i add the <b> tag to the text that will appear when onclick is executed(ChangeTxt function) the <b> tag is not taken into account, only the <br> tag works, i need also to put <spans> in this text, how can i make so tags are taken into account?
    Last edited by tetrix; 08-15-2008 at 11:34 AM.

  2. #2
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    Did you forget your </b> and </span>s?
    Edit: Please don't start a debate over whether it's <span>s or <spans>.
    ....(o_ Penguins
    .---/(o_- techno_racing
    +(---//\-' in
    .+(_)--(_)' The McMurdo 500

  3. #3
    Join Date
    Nov 2007
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    hi here is the example again: i added a span in the text and the function does not work for that onclick:


    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    <title></title>
    
    <script language="JavaScript" type="text/javascript">
    function ChangeTxt(txt) {
    document.getElementById('ref').innerHTML = txt;
    }
    </script>
    
    </head>
    
    
    
    
    
    <body>
    
    
    <table id="thumbnails">
    <tr>
    <td><a href="#nogo" onClick="ChangeTxt('oneoneoneone<b>one</b>oneone')">one</a></td>
    <td><a href="#nogo" onClick="ChangeTxt('two<span style='color:red'>twotwotwo</span>twotwotwo')">two</a></td>
    <td><a href="#nogo" onClick="ChangeTxt('threethreethreethreethree')">three</a></td>
    </tr>
    </table>
    
    
    
    <div id="ref">oneoneoneone<b>one</b>oneone</div>
    
    
    
    </body>
    
    </html>

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You need to escape the quotes:
    Code:
    onClick="ChangeTxt('two<span style=\'color:red\'>twotwotwo</span>twotwotwo')">
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yes, you must escape any contained delimiters as rangana says, it doesn't hurt to escape closing slashes (/) too, as not doing so can confuse the browser, and will make your code invalid (usually there is no problem, so people often skip this). Also you should return false. If you are using this in several places, the easiest way is to return the function (get rid of the language attribute - it's deprecated):

    Code:
    <script type="text/javascript">
    function ChangeTxt(txt) {
    document.getElementById('ref').innerHTML = txt;
    return false;
    }
    </script>
    The when you use it (closing slashes escaped, return added):

    Code:
    <td><a href="#nogo" onclick="return ChangeTxt('oneoneoneone<b>one<\/b>oneone')">one</a></td>
    <td><a href="#nogo" onclick="return ChangeTxt('two<span style=\'color:red\'>twotwotwo<\/span>twotwotwo')">two</a></td>
    <td><a href="#nogo" onclick="return ChangeTxt('threethreethreethreethree')">three</a></td>
    The return is very important because many browsers will still execute the link if the onclick event doesn't return false. Even though your link is to a named anchor, this will still reload the page in many browsers, wiping out any changes that the event made to the document.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Nov 2007
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    thanks a lot for the explanations.

  7. #7
    Join Date
    Nov 2007
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    would it be possible to add something like this inside the function so to create a table?

    <table cellpadding="0" cellspacing="0" border="0">
    <tr>
    <td></td>
    </tr>
    </table>

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Note that innerHTML is non-standard and should be avoided wherever possible.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Twey is of course correct here. However, many people find it simply easier to use the non-standard, but well supported innerHTML rather than the standard DOM 2 methods for updating content dynamically.

    There are pitfalls if one goes the innerHTML route. If you give the browser incomplete tags, it may complete (close) them for you. Then if you later close them with a subsequent innerHTML directive, it can often lead to unexpected errors. Using innerHTML with forms, even when done so 'correctly', can lead to errors and/or unexpected results.

    Tables are another area where odd things can happen. However, owing to the vagaries of support in various browsers, sometimes innerHTML is better for tables. But better still is using table's rows and cells collections and methods.

    Getting back to your question:

    would it be possible to add something like this inside the function so to create a table?

    HTML Code:
    <table cellpadding="0" cellspacing="0" border="0">
    <tr>
    <td></td>
    </tr>
    </table>
    Yes you can do that. The best way (if using innerHTML) is to concatenate the entire desired markup to a variable, and then assign it to the target element's innerHTML. Otherwise you risk some of the pitfalls I mentioned earlier.

    Now, I doubt that you really want to create:

    HTML Code:
    <table cellpadding="0" cellspacing="0" border="0">
    <tr>
    <td></td>
    </tr>
    </table>
    because that wouldn't look any different than:

    HTML Code:
    <br>&nbsp;
    So, just what is it that you want to do?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •