/    Sign up×
Community /Pin to ProfileBookmark

set innerHTML or … ??????

I have a form for users to enter events and their start and end times. There is a link they can click to add additional days to their event if it’s a multi-day event. To add the extra form inputs, i’m setting the innerHTML attribute of a div tag. it’s working! on both firefox and IE.

I’m wondering if this is ok or if there is something inherently bad about this or if there’s a better way.

I’m using PHP so those weird tags you may not recognize are supposed to be there.

script:

[CODE]
<script language=”Javascript”>
var numberOfDays = 1;
// this fn adds another day to the form
function addDay() {
if (!document.getElementById) {
alert(“Your browser does not support this feature. Please get a more modern browser”);
return;
}
var dayOneEl = document.getElementById(‘day_one’);
dayOneEl.style.display = ‘inline’;
var sameDateEl = document.getElementById(‘same_time_each_day_yes’);
var dateEl = document.getElementById(‘date_div’);
numberOfDays++;
var currentHTML = ”;
currentHTML += ‘<br>’;
currentHTML += ‘Day ‘ + numberOfDays + ‘<br>’;
currentHTML += ‘<table width=”100%” cellpadding=”0″ cellspacing=”0″ border=”0″><tr><td align=”left” valign=”top” width=”25%”><input name=”start_date[‘ + numberOfDays + ‘]” id=”start_date_’ + numberOfDays + ‘” type=”text” value=”” size=”10″>&nbsp;<a href=”javascript:void(0);” onClick=”calendarPopup(‘start_date_’ + numberOfDays + ”);”><img src=”images/calendar_icon.jpg” width=”16″ height=”15″ border=”0″></a></td>’;
currentHTML += ‘<td align=”left” valign=”top”><table width=”100%” border=”0″ cellpadding=”0″ cellspacing=”0″ id=”event_time_’ + numberOfDays + ‘”‘;
if (sameDateEl.checked == true) {
currentHTML += ‘ style=”display:none;”‘;
}

currentHTML += ‘>’;
currentHTML += ‘<tr>’;
currentHTML += ‘<td width=”10%” align=”right”><b><font color=”#FF0000″>*</font></b>Start&nbsp;Time:</td>’;
currentHTML += ‘<td align=”left” valign=”top”><select name=”start_time[‘ + numberOfDays + ‘]” size=”1″><option value=””>&ndash;</option><option value=”0″>12</option>’;
for(i=1; i<12; i++) {
currentHTML += ‘<option value=”‘ + i + ‘”>’ + i + ‘</option>’;
}
currentHTML += ‘</select>&nbsp;’;
currentHTML += ‘<input type=”radio” name=”start_time_m[‘ + numberOfDays + ‘]” value=”0″>am <input type=”radio” name=”start_time_m[‘ + numberOfDays + ‘]” value=”12″>pm</td>’;
currentHTML += ‘</tr>’;
currentHTML += ‘<tr>’;
currentHTML += ‘<td align=”right” valign=”top”><b><font color=”#FF0000″>*</font></b>End&nbsp;Time:</td>’;
currentHTML += ‘<td align=”left” valign=”top”><select name=”end_time[‘ + numberOfDays + ‘]” size=”1″><option value=””>&ndash;</option><option value=”0″>12</option>’;
for(i=1; i<12; i++) {
currentHTML += ‘<option value=”‘ + i + ‘”>’ + i + ‘</option>’;
}
currentHTML += ‘</select>&nbsp;’;
currentHTML += ‘<input type=”radio” name=”end_time_m[‘ + numberOfDays + ‘]” value=”0″>am <input type=”radio” name=”end_time_m[‘ + numberOfDays + ‘]” value=”0″>pm</td>’;

currentHTML += ‘</tr>’;
currentHTML += ‘</table></td></tr></table>’;
dateEl.innerHTML += currentHTML;

var elST = document.getElementById(‘same_time_each_day’);
elST.style.display = ‘block’;
} // addDay

function sameTimeChange() {
if (!document.getElementById) {
alert(“Your browser does not support this feature. Please get a more modern browser”);
return;
}
var sameDateEl = document.getElementById(‘same_time_each_day_yes’);
if (numberOfDays < 2) {
return;
}
for(i=2;i<=numberOfDays;i++) {
var el = document.getElementById(‘event_time_’ + i);
if (sameDateEl.checked == true) {
el.style.display = “none”;
} else {
el.style.display = “block”;
}
}
}
</script>
[/CODE]

form:

[code=html]
<table>
<tr>
<td id=”date_element” align=”left” valign=”top”><span id=”day_one” style=”display:none;”>Day 1<br></span>
<table width=”100%” cellpadding=”0″ cellspacing=”0″ border=”0″>
<tr>
<td align=”left” valign=”top” width=”25%”><input name=”start_date[1]” id=”start_date_1″ type=”text” value=”” size=”10″>&nbsp;<a href=”javascript:void(0);” onClick=”calendarPopup(‘start_date_1’);”><img src=”images/calendar_icon.jpg” width=”16″ height=”15″ border=”0″></a></td>
<td align=”left” valign=”top”><table width=”100%” cellpadding=”0″ cellspacing=”0″ border=”0″ id=”event_time_1″>
<tr>
<td width=”10%” align=”right”><b><font color=”#FF0000″>*</font></b>Start&nbsp;Time:</td>
<td align=”left” valign=”top”><select name=”start_time[1]” size=”1″>
<option value=””>&ndash;</option>
<option value=”0″<?=($_POST[‘start_time’][1] === ‘0’) ? ‘ selected’ : ” ?>>12</option>
<?
for($i=1; $i<12; $i++) {
echo ‘<option value=”‘ . $i . ‘”‘ . (($_POST[‘start_time’][1] == $i) ? ‘ selected’ : ”) . ‘>’ . $i . ‘</option>’ . chr(10);
}
?>
</select>&nbsp;<input type=”radio” name=”start_time_m[1]” value=”0″<?=($_POST[‘start_time_m’][1] === ‘0’) ? ‘ checked’ : ” ?>>am
<input type=”radio” name=”start_time_m[1]” value=”12″<?=($_POST[‘start_time_m’][1] == ’12’) ? ‘ checked’ : ” ?>>pm</td>
</tr>
<tr>
<td align=”right” valign=”top”><b><font color=”#FF0000″>*</font></b>End&nbsp;Time:</td>
<td align=”left” valign=”top”><select name=”end_time[1]” size=”1″>
<option value=””>&ndash;</option>
<option value=”0″<?=($_POST[‘end_time’][1] === ‘0’) ? ‘ checked’ : ” ?>>12</option>
<?
for($i=1; $i<12; $i++) {
echo ‘<option value=”‘ . $i . ‘”‘ . (($_POST[‘end_time’][1] == $i) ? ‘ selected’ : ”) . ‘>’ . $i . ‘</option>’ . chr(10);
}
?>
</select>&nbsp;<input type=”radio” name=”end_time_m[1]” value=”0″<?=($_POST[‘end_time_m’][1] === ‘0’) ? ‘ checked’ : ” ?>>am
<input type=”radio” name=”end_time_m[1]” value=”0″<?=($_POST[‘end_time_m’][1] == ’12’) ? ‘ checked’ : ” ?>>pm</td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table>

[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@toicontienOct 03.2006 — There's nothing inherently bad with how you're doing it. If the list gets really long, you'll start noticing more of a lag when you add an item, but that lag might not be noticeable until you've got 100 items in the list. Basically, if it works cross browser and performs well, then keep it.
Copy linkTweet thisAlerts:
@crushmeguyOct 04.2006 — If you care about practicality alone, innerHTML is probably fine. Another, more sophisticated method would be to continue adding elements to the DOM directly with the appendChild method.

One thing I strongly suggest you do is take all layout elements out of your HTML, such as font tags and "width" attributes. Instead, use a stylesheet. Your code will become significantly easier to read (and easier to maintain).

Doug
Copy linkTweet thisAlerts:
@sneakyimpauthorOct 04.2006 — phew. i'm glad this isn't a stupid way to do it. the DOM model has been so tough for me. i get it working on one browser and another it doesn't work.

that's a good tip about using stylenames. i haven't had a lot of luck getting table widths to work using stylesheets but i see what you mean. the javascript *is* pretty nasty.

at the moment, i'm having that age-old problem where the images referenced in the added html don't show up in internet explorer.
Copy linkTweet thisAlerts:
@KorOct 04.2006 — DOM model is wise and precise. But innerHTML (which is a crossbrowser method, even it is not a standard one) is faster. It depend on needs.

If you need to insert the new elements as real childNodes into the document's DOM tree (that means later you might need to use DOM reference to do something dynamic with them), use DOM. If you need simply to insert a HTML code and you have a huge number of elements, probably innerHTML will do it faster.
×

Success!

Help @sneakyimp 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.24,
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,
)...