SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    script problem in ff

    can you help me, why does this script work in IE but not in FF? My script is like this:

    PHP Code:
    <html>
    <
    head>
    <
    title>123</title>
    <
    script type="text/javascript">
    // <![CDATA[ //
        
    var clicked 0;
        
        function 
    addBoldTag(){
            var 
    tArea document.forms["myForm"].myTextArea;
            
            
    clicked++;
            
            if(
    tArea.hasChildNodes()){
                var 
    tag = (clicked != 0) ? "<b>" "</b>";
                
    tArea.firstChild.nodeValue += tag;
            }else{
                var 
    tag = (clicked != 0) ? "<b>" "</b>";
                var 
    newText document.createTextNode(tag);
                
    tArea.appendChild(newText);
            }
        }
    // ]]> //
    </script>
    </head>

    <body>
    <form name="myForm">
    <img value="bold" src="bold.gif" onclick="addBoldTag()" /><br />
    <textarea name="myTextArea" cols="30" rows="10"></textarea>
    </form>
    </body>
    </html> 

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does the JavaScript console in Firefox tell you

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nothing, I think... See for yourself here: http://jauninjais.eclub.lv/body.html

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have Firefox here to test, but personally I wouldn't use the DOM for this. Try this instead:
    Code:
        var clicked = 0; 
         
        function addBoldTag(){ 
            var tArea = document.forms["myForm"].myTextArea; 
             
            clicked++; 
             
            var tag = (clicked % 2 != 0) ? "<b>" : "</b>"; 
            tArea.value += tag; 
        }

  5. #5
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tnx, it works only one question how can I put <b> where my cursor is, not at the end of all the text?

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,834
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Your values for tag are invalid and you can't include tags in text nodes because tags are not text. To create a bold container into which things can be put you would use var newb = document.createElement("b");
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


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
  •