Results 1 to 4 of 4

Thread: JavaScript - How to create Element

  1. #1
    Join Date
    Jun 2009
    Posts
    321

    JavaScript - How to create Element

    I need to create an 'a' element and append it to a div tag. I had already made a script that dynamically adds checkboxes and hidden input type to a given page but the problem is it works with IE but not with FireFox. Could any one of make me how to do that?

  2. #2
    Join Date
    Feb 2009
    Posts
    233

    Re: JavaScript - How to create Element

    When assigning some HTML to the innerHTML property of a DOM element, Internet Explorer performs some security text, especially if the inserted HTML contains javascript. To make your script with Firefox, you can try this :-



    function test(){

    var hr =document.createElement("hr");
    document.getElementById("thebody").appendChild(hr);

    }
    Or better you try this code create an 'a' element and append it to a div tag:-

    <script type="text/javascript">

    function sneakInLink()
    {

    var myLink = document.createElement('a');

    var href = document.createAttribute('href');

    myLink.setAttribute('href','http://www.afcc1.com');

    myLink.innerText ="Go here";

    var spanAppend = document.getElementById('myDivLink');

    spanAppend.appendChild(myLink);

    } <= ***here you were using {***

    </script> <body onload="sneakInLink()">

    <div id="myDivLink"></div>***<= here was the tag missing?***
    </body>

  3. #3
    Join Date
    Feb 2009
    Posts
    391

    Re: JavaScript - How to create Element

    you have to use the javascript: prefix on the href for it to become an active javascript function caller. So this is how it should be:


    newlink = document.createElement('a');
    newlink.setAttribute('class', 'signature');
    newlink.setAttribute('href', 'javascript:showSignature(xyz)');

  4. #4
    Join Date
    Feb 2009
    Posts
    192

    Re: JavaScript - How to create Element

    Create element Script code:-

    <html>
    <body>
    <script language="javascript">
    function function1(){
    var myElement = document.createElement('<hr>');
    myBody.appendChild(myElement);

    myElement = document.createElement('<Br>');
    myBody.appendChild(myElement);

    myElement = document.createElement('<Hr>');
    myBody.appendChild(myElement);
    }
    </script>
    <body id="myBody">
    <button onClick="function1();">Put horizontal rule</button>
    </body>
    </html>

Similar Threads

  1. Replies: 4
    Last Post: 26-03-2012, 10:59 PM
  2. javascript get element by class
    By GlassFish in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 09:05 PM
  3. How to set the value of a form element using Javascript?
    By shivendra in forum Software Development
    Replies: 4
    Last Post: 07-02-2010, 05:04 AM
  4. Code to scroll to javascript element.
    By sizzla in forum Software Development
    Replies: 2
    Last Post: 04-07-2009, 10:13 PM
  5. How do I know the width of an element through javascript?
    By Conner in forum Software Development
    Replies: 3
    Last Post: 06-04-2009, 11:29 AM

Tags for this Thread

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
  •  
Page generated in 1,713,254,499.11516 seconds with 17 queries