/    Sign up×
Community /Pin to ProfileBookmark

Is it possible to generate form objects from JavaScript?

Is it possible to generate an Html form object from a JavaScript function? Because I have a dropdownlist (a Select form object in asp.net vb.net page) in which the user has to choose an option, and if he/she choose the option 7 an ‘input text’ form object has to appear, and if she/he choose whatever option after choose option 7 it has to disappear. For instance something like this:

[code]
function checkOtherAct() {

var userAct = document.formName.userA.value
var myTD = document.getElementById(“specificAct”);

switch(userAct) {

case “0”:
myTD… //No input text
break;

case “1”:
myTD… //No input text
break;

case “7”:
myTD… //An input text
break;

default:
break;

}
}
checkOtherAct();
[/code]

On the form:

[code]
<td><asp:dropdownlist id=”userA” onChange=”checkOtherAct()” runat=”server”…></asp:dropdownlist></td>
<td id=”specificAct”> </td>
[/code]

Thanks

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@olafMar 19.2004 — of course just use document.write("<input name=...")
Copy linkTweet thisAlerts:
@erick30authorMar 19.2004 — [i]Originally posted by olaf [/i]

[B]of course just use document.write("<input name=...") [/B][/QUOTE]


I have tested this function:
<i>
</i>function checkAct() {

var otherAct = document.formName.activType.value
var myTD = document.getElementById("what");

<i> </i> switch(otherAct) {

<i> </i> case "0":
<i> </i> myTD.document.write("");
<i> </i> break;

<i> </i> case "1":
<i> </i> myTD.document.write("");
<i> </i> break;
<i> </i> ...

<i> </i> case "7":
<i> </i> myTD.document.write("&lt;input type='text' name='specificAct'&gt;");
<i> </i> break;

<i> </i> default:
<i> </i> break;

<i> </i> }
}
checkAct();


On the form:
<i>
</i>&lt;td&gt;&lt;asp:dropdownlist id="userA" onChange=”checkAct();” runat="server"...&gt;&lt;/asp:dropdownlist&gt;&lt;/td&gt;
&lt;td id="what"&gt; &lt;/td&gt;


And I receive this error:

[I]

Error: '__ EVENT TARGET' is null or it isn' t an object

Code: 0

Url: about:blank

[/I]
Copy linkTweet thisAlerts:
@olafMar 19.2004 — Set a boolean on true and use an if clause where you want to show the field...
Copy linkTweet thisAlerts:
@erick30authorMar 19.2004 — [i]Originally posted by olaf [/i]

[B]Set a boolean on true and use an if clause where you want to show the field... [/B][/QUOTE]


Sorry but I do not know how can I do this. Would you mind writing it just in my code example please?
Copy linkTweet thisAlerts:
@olafMar 19.2004 — Sorry, but i have no knowledge about ASP
Copy linkTweet thisAlerts:
@erick30authorMar 19.2004 — I think that you don' t need knowledge about ASP, since the (onChange=”checkAct();”...) is the same as we use in html, and the (<td id="what"></td>) is html.
Copy linkTweet thisAlerts:
@JayDieMar 19.2004 — Take a look at:

[URL]http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/createelement.asp[/URL]

The you can set attributes with 'setAttribute' and create the text inside with innerHTML.

Look at the insert.... methods to insert you element.

JayDie
Copy linkTweet thisAlerts:
@erick30authorMar 19.2004 — Good, now runs, thank you ! ?. A little thing more, You know the span element in Html? How can I put a 'span class' from the function? See the 'case:7', I have put it thus but in the page appears '[object]' instead of a red asterisk.

<i>
</i>function checkAct() {

var otherAct = document.formName.activType.value
var myTD = document.getElementById("what");
var myTD2 = document.getElementById("title");

<i> </i> switch(otherAct) {

<i> </i> case "0":
<i> </i> myTD2.innerHTML = "";
<i> </i> myTD.innerHTML = "";
<i> </i> break;

<i> </i> case "1":
<i> </i> myTD2.innerHTML = "";
<i> </i> myTD.innerHTML = "";
<i> </i> break;

<i> </i> ...


<i> </i> case "7":
<i> </i> myTD2.innerHTML = document.createElement("&lt;span class='astRed'&gt;*&lt;/span&gt;") + "Activity:";
<i> </i> var whatAct = document.createElement("&lt;INPUT TYPE='text' NAME='actS'&gt;")
<i> </i> myTD.insertBefore(whatAct);


<i> </i> break;

<i> </i> default:
<i> </i> break;

<i> </i> }
}
checkAct();
×

Success!

Help @erick30 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.18,
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,
)...