/    Sign up×
Community /Pin to ProfileBookmark

Making IE script work in Firefox.

Can any Firefox pros tell me why this function works in IE but not Firefox?
function addtbl()
{
idx++
var mydiv = document.getElementById(“d1”);
var thtm = “<table width =’100%’ id=’tbl” + idx + “A’>”
var newtA = document.createElement(thtm);
mytablebody = document.createElement(“tbody”);
var newrA = document.createElement(‘tr’);
var newcA = document.createElement(“<td width=’10%’ style=’font-weight: bold’>”);
var newtxtA = document.createTextNode(“Group ” + idx);
var newcA1 = document.createElement(“td”);
var newtxtA1 = document.createTextNode(“There will be “);
var newcA2 = document.createElement(“<select id=’numCBT” + idx + “‘ onchange=’popCBs(” + idx + “)>”);
var newtxtA2 = document.createTextNode(” checkboxes in this group. Once a checkbox is selected, the users “);
var newcA3 = document.createElement(“<select id=’bln_UnChkAll” + idx + “‘>”);
var newtxtA3 = document.createTextNode(” uncheck all checkboxes in the group.”);
newcA1.appendChild(newtxtA1);
newcA1.appendChild(newcA2);
newcA1.appendChild(newtxtA2);
newcA1.appendChild(newcA3);
newcA1.appendChild(newcA3);
newcA1.appendChild(newtxtA3);
newcA.appendChild(newtxtA);
newrA.appendChild(newcA);
newrA.appendChild(newcA1);
mytablebody.appendChild(newrA);
newtA.appendChild(mytablebody);
mydiv.appendChild(newtA);
var thtm = “<table width =’100%’ id=’tbl” + idx + “‘>”
var newt = document.createElement(thtm);
mydiv.appendChild(newt)
document.getElementById(“d1”).style.display=’inline’;
eval(“popDDs(” + idx + “)”)
return newtA
}
Is there an online documentation repository for Firefox? (like msdn for Microsoft products)

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@mrhooFeb 16.2007 — This syntax is IE only:

document.createElement("<select id='numCBT" + idx + "' onchange='popCBs(" + idx + ")>");

Other browsers use document.createElement('select');

and then setAttribute for the new element's attributes.

http://www.mozilla.org/
Copy linkTweet thisAlerts:
@UltimaterFeb 16.2007 — Untested. Very high chance it will work flawlessly since I rarely have to debug my JavaScript.
<i>
</i>function addtbl()
{
idx++
var mydiv = document.getElementById("d1");
try{
var newtA = document.createElement("&lt;table width ='100%' id='tbl" + idx + "A'&gt;");
}catch(E){
var newtA = document.createElement("table");
newta.setAttribute("width","100%")
newta.id="tbl"+idx+"A";
}

mytablebody = document.createElement("tbody");
var newrA = document.createElement('tr');
try{
var newcA = document.createElement("&lt;td width='10%' style='font-weight: bold'&gt;");
}catch(E){
var newcA = document.createElement("td");
newcA.setAttribute("width","10%");
newcA.style.fontWeight="bold";
}

var newtxtA = document.createTextNode("Group " + idx);
var newcA1 = document.createElement("td");
var newtxtA1 = document.createTextNode("There will be ");
try{
var newcA2 = document.createElement("&lt;select id='numCBT" + idx + "' onchange='popCBs(" + idx + ")&gt;");
}catch(E){
var newcA2 = document.createElement("select");
newcA2.id="numCBT"+idx;
newcA2.onchange=new Function("popCBs(" + idx + ")");
}

var newtxtA2 = document.createTextNode(" checkboxes in this group. Once a checkbox is selected, the users ");

try{
var newcA3 = document.createElement("&lt;select id='bln_UnChkAll" + idx + "'&gt;");
}catch(E){
var newcA3 = document.createElement("select");
newcA3.id="bln_UnChkAll"+idx;
}

var newtxtA3 = document.createTextNode(" uncheck all checkboxes in the group.");
newcA1.appendChild(newtxtA1);
newcA1.appendChild(newcA2);
newcA1.appendChild(newtxtA2);
newcA1.appendChild(newcA3);
newcA1.appendChild(newcA3);
newcA1.appendChild(newtxtA3);
newcA.appendChild(newtxtA);
newrA.appendChild(newcA);
newrA.appendChild(newcA1);
mytablebody.appendChild(newrA);
newtA.appendChild(mytablebody);
mydiv.appendChild(newtA);
try{
var newt = document.createElement("&lt;table width ='100%' id='tbl" + idx + "'&gt;");
}catch(E){
var newt = document.createElement("table");
newt.setAttribute("width","100%");
newt.id="tbl"+idx;
}
mydiv.appendChild(newt)
document.getElementById("d1").style.display='inline';
popDDs(idx);
return newtA
}


IE has issues with setting NAMEs and IDs dynamically on elements so IE needs to be served by specifying them within createElement. However browsers like Firefox may support createElement("<br>") however if you try to add any attributes along with it, an error will be generated. Hence the reason to use a try-catch block for cross-browser compatibility and the accepted way of doing things by the gurus.
Copy linkTweet thisAlerts:
@felgallFeb 16.2007 — The DOM has a whole lot of special commands specifically for handling tables so that you don't have to use createElement for the caption, thead, tfoot, tbody, th, tr, and td parts but can reference the appropriate properties within the table element that you created.

See http://javascript.about.com/library/bldom15.htm for a list of the DOM methods available to use with tables

It may also be more efficient if instead of using a try block to determine whether the IE or standard version of the create will work that you instead place the code inside of JScript conditionals so that one version runs for JScript (IE) and the other runs for Javascript (all other browsers except IE).

See http://javascript.about.com/library/bljscript1.htm for examples of JScript conditionals.
×

Success!

Help @davidmal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...