/    Sign up×
Community /Pin to ProfileBookmark

Why can’t this javascript run ?

Dear Sir,
The following code is to dynamically generate textbox. But I met some problems when running the javascript code:
d.innerHTML+=form.dir.options[form.dir.selectedIndex].value;

What’s the problem ?

<html>

<head>
<title></title>
<script>
function generateRow(form)
{
var d=document.getElementById(“div”);
d.innerHTML+=form.dir.options[form.dir.selectedIndex].value;
d.innerHTML+=”<input type=’text’ name=’itemCode’ size = ‘5’><br>” ;
}
</script>
</head>
<body>
<table BORDER=1 CELLSPACING=0 CELLPADDING=3 align=center width=600 bordercolorlight=#99FF99>
<td align = center width = ‘300’>
<div id=”div”></div>
</td>
<td>
<form name = “mydir”>
<div style=”position:absolute;left: 600; top: 0″>

<select name = “dir”>
<option>11</option>
<option>22</option>
</select>
<input type=”button” value=”&#26032;&#22686;&#27396;&#20301;” onclick=”generateRow(this.form)”>
<Input type = “Submit” Name = “&#20786;&#23384;” value = “&#20786;&#23384;” >
</form>
</td>
</table>

</body>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@MjhLkwdSep 17.2005 — Robert:

You didn't declare "values" for the select list options. And "form" is a reserved word, you shouldn't use it within your code.

You can use alert() to display the value of variables. I commented out the line of code where I used it.

I made the changes, and this seems to work.


[CODE]<html>

<head>
<title></title>
<script>
function generateRow(nForm)
{
var d=document.getElementById("div");
d.innerHTML+=nForm.dir.value;
//alert(nForm.dir.value)
d.innerHTML+="<input type='text' name='itemCode' size = '5'><br>" ;
}
</script>
</head>
<body>
<table BORDER=1 CELLSPACING=0 CELLPADDING=3 align=center width=600 bordercolorlight=#99FF99>
<td align = center width = '300'>
<div id="div"></div>
</td>
<td>
<form name = "mydir">
<div style="position:absolute;left: 600; top: 0">
<select name = "dir">
<option value='11'>11</option>
<option value='22'>22</option>
</select>
<input type="button" value="????" onclick="generateRow(this.form)">
<Input type = "Submit" Name = "??" value = "??" >
</form>
</td>
</table>
</body>[/CODE]
Copy linkTweet thisAlerts:
@Robert_ChuauthorSep 18.2005 — Thank you.
Copy linkTweet thisAlerts:
@Robert_ChuauthorSep 18.2005 — Dear Sir,

I want to add the textbox in a table row by row. So I changed my code to the following. But it didn't show anything. How can I make it run ?

d.innerHTML+="<tr><td>".nForm.dir.value;

d.innerHTML+="<input type='text' name='itemCode' size = '5'></tr></td>" ;

Here is the complete code.

<html>

<head>

<title></title>

<script>

function generateRow(nForm)

{

var d=document.getElementById("div");

d.innerHTML+="<tr><td>".nForm.dir.value;

//alert(nForm.dir.value)

d.innerHTML+="<input type='text' name='itemCode' size = '5'></tr></td>" ;

}

</script>

</head>

<body>

<table BORDER=1 CELLSPACING=0 CELLPADDING=3 align=center width=600 bordercolorlight=#99FF99>

<td>

<table>

<div id="div"></div>

</table>

</td>

<td>

<form name = "mydir">

<div style="position:absolute;left: 600; top: 0">

<select name = "dir">

<option value='11'>11</option>

<option value='22'>22</option>

</select>

<input type="button" value="????" onclick="generateRow(this.form)">

<Input type = "Submit" Name = "??" value = "??" >

</form>

</td>

</table>

</body>__________________
×

Success!

Help @Robert_Chu 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...