SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    function not working in mozilla

    i've this function:
    HTML Code:
           function strong() {
            document.getElementById("texto").innerHTML = 
             document.getElementById("texto").innerHTML + 
             '<strong></strong>';
        }
    i call it like this:
    HTML Code:
    <input type = "radio" name = "RadioHtml" value = "strong"   onclick = "strong()"/>
    and it is suposed to insert that tag inside my textarea; well, it does so with ie6, but not with mozilla firefox.Why?

    TiA

  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)
    Textareas dont contain HTML, use the value property instead.

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm sorry but i dont get it: you say textareas dont have html, but i use a textarea to input content that later on will be displayed as 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)
    Yes, but it is still text right? The value property is a very basic property for form elements. Check it out.

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE returns the text from a textarea or text input with innerHTML,
    the other browsers do not.

    innerHTML is not a standard- it was once IE only, now most other browsers also have an innerHTML property, but they implement it any way they choose.

    Write this for IE only, or use methods that are not proprietory to change the content of the textarea.

  6. #6
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am visiting this forum few days and this question is here quiet often, so again code based on this genial code here http://alexking.org/blog/2003/06/02/...ng-javascript/

    Code:
    <?xml version="1.0" encoding="windows-1250"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=windows-1250" />
    <title>Title</title>
    <style type="text/css"><!--
    #ovladace{display:none;}
    --></style>
    <script type="text/javascript">
    var d=document;
    function tarea(startTag,endTag) {
    var txt=d.getElementById('txt');
    if(d.selection){//ie
    sel=d.selection.createRange();
    sel.text=startTag+sel.text+endTag;}
    else if(txt.selectionStart||txt.selectionStart=='0'){//gecko
    var zac=txt.selectionStart;
    var kon=txt.selectionEnd;
    txt.value=txt.value.substring(0,zac)
    +startTag
    +txt.value.substring(zac,kon)
    +endTag
    +txt.value.substring(kon,txt.value.length);
    txt.selectionStart=zac+startTag.length;
    txt.selectionEnd=kon+startTag.length;}
    else{txt.value+=myValue;}
    txt.focus();}
    function init(){
    var kde=d.getElementById('ovladace');
    kde.style.display='block';
    var hrefy=kde.getElementsByTagName('a');
    for(i=0,j=0;i<hrefy.length;i++){
    hrefy[i].onclick=function(){
    tarea('['+this.getAttribute('title')+']','[/'+this.getAttribute('title')+']');
    //in previous line replace [ with &lt; and ] with  &gt;
    return false;}}}
    if(document.childNodes){window.onload=init}
    </script>
    </head>
    <body>
    <textarea id="txt"></textarea>
    <div id="ovladace">
    <a href="#" title="h1">h1</a>
    <a href="#" title="h2">h2</a>
    <a href="#" title="h3">h3</a>
    <a href="#" title="h4">h4</a>
    <a href="#" title="h5">h5</a>
    </div>
    </body>
    </html>

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm still in the process of learning js; i know (from a book i have) that i can navigate and alter the value;
    i tried:
    HTML Code:
    var conteudo = document.getElementById("texto").firstChild.nodeValue += '&lt;strong&gt;&lt;/strong&gt;';
    but still does not work with mozilla...

  8. #8
    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)
    rlf. ok, what you need to set is the value property, like this
    Code:
    var myTextArea = document.getElementById("texto");
    myTextArea.value = "this text will appear inside the textarea.";

  9. #9
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i got it
    thanks a lot to all who helped!
    HTML Code:
        function strong() {
          var conteudo = document.getElementById("texto");
          conteudo.value += '&lt;strong&gt;&lt;/strong&gt;';
        }

  10. #10
    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)
    Never ever under-estimate the power of setting a form fields value


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
  •