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?
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 :-
Quote:
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:-
Quote:
<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>
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:
Quote:
newlink = document.createElement('a');
newlink.setAttribute('class', 'signature');
newlink.setAttribute('href', 'javascript:showSignature(xyz)');
Re: JavaScript - How to create Element
Create element Script code:-
Quote:
<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>