Click to See Complete Forum and Search --> : form function


bddosch
05-08-2003, 10:32 AM
<script language="JavaScript">
<!--
function checkField(object) {
if (document.Order.object.checked) {
alert("right");
}else{
alert("wrong");
}
}
//-->
</script>

<input name="check" type="checkbox" id="check" value="checkbox" onClick="checkField(check);">

this is the format I want to keep my script and make the "object" dynamic. I have tried the folowing.

<!--
function checkField(object) {
if ("document.Order." + object + ".checked") {
alert("right");
}else{
alert("wrong");
}
}
//-->
</script>

Jona
05-08-2003, 10:38 AM
onClick="checkField(this)"

pyro
05-08-2003, 10:38 AM
Try this:

<script language="javascript" type="text/javascript">
function checkField(object) {
if (object.checked == true) {
alert("right");
}
else {
alert("wrong");
}
}
</script>
</head>
<body>
<form name="Order">
<input name="check" type="checkbox" id="check" value="checkbox" onClick="checkField(this);">
</form>

Jona
05-08-2003, 10:39 AM
LOL, Pyro! Cross-post.. :p

pyro
05-08-2003, 10:40 AM
lol...yeah, but I also pointed out that he needs to reference the form object differently in his if command, which he will need to know. ;)

Jona
05-08-2003, 10:42 AM
Well, you're right and wrong. Your code works, but his (minus the document.Order part) did, too. You don't necessarily have to have the == true part. if(object.checked){ works fine. :)

pyro
05-08-2003, 10:43 AM
Not to get off on a rabbit trail, but how does adding that in make me wrong? It's just a different way of doing it. I just meant to point out that he needed to remove the document.Order if he was referencing the form field with a this...

Jona
05-08-2003, 10:45 AM
OK, my bad. You were right, but there's always a different way to do things. :p

bddosch
05-08-2003, 10:46 AM
The problem with this is guys.... I do not want to get rid of the document.Form part.... Is there a way?????

pyro
05-08-2003, 10:51 AM
Yeah, like this:

<script language="javascript" type="text/javascript">
function checkField() {
if (document.Order.check.checked == true) {
alert("right");
}
else {
alert("wrong");
}
}
</script>
</head>
<body>
<form name="Order">
<input name="check" type="checkbox" id="check" value="checkbox" onClick="checkField();">
</form>

bddosch
05-08-2003, 10:55 AM
the things is:


<script language="javascript" type="text/javascript">
function checkField(objectCheckbox) {
if (document.Order.objectCheckbox.checked == true) {
alert("right");
}
else {
alert("wrong");
}
}
</script>
</head>
<body>
<form name="Order">
<input name="check" type="checkbox" id="check" value="checkbox" onClick="checkField('check');">

<input name="check" type="checkbox" id="check" value="checkbox" onClick="checkField('check2');">
</form>

I want to make the "check" bit dynamic so that I could use the same funtion for say another checkbox called "check2"..

pyro
05-08-2003, 10:56 AM
You could try doing it like this:

<script language="javascript" type="text/javascript">
function checkField(obj) {
if (document.getElementById(obj).checked == true) {
alert("right");
}
else {
alert("wrong");
}
}
</script>
</head>
<body>
<form name="Order">
<input name="check" type="checkbox" id="check" value="checkbox" onClick="checkField(this.id);">
</form>

bddosch
05-08-2003, 11:01 AM
this has worked fine.....

cool man..

fakeName
05-08-2003, 11:20 AM
I know that this has been solved, but if you are also worrying about Netscape 4.x not understanding getElementById, you can pass the name from the checkbox object to the function using eval(), which seems to be what you were trying to do in your first post.

<script language="JavaScript">
<!--
function checkField(object) {
if (eval("document.Order." +object+ ".checked")) {
alert("right");
}else {
alert("wrong");
}
}
//-->
</script>

</head>
<body>
<form name = "Order">
<input name="check" type="checkbox" id="check" value="checkbox" onClick="checkField('check');"><br>
</form>
</body>

bddosch
05-09-2003, 03:17 AM
This has worked much better cause it is working with my next function as well

for (i=0;i<(eval("document.Order." +radio+ ".length"));i++) {
if (document.Order.Fx[i].checked) {
fxtotalamount=(document.Order.Fx[i].value)
}
}

Thanks for that Fakename..................

bddosch
05-09-2003, 04:07 AM
The only other problem is:

function paperTotal(obj,code) {

FXtotalamount=(eval("document.Order." +code+ "[i].value"))

}

the "code" basiclay gets its value as "FX"

how do I get +code+ to go

+code+totalamount=(eval("document.Order." +code+ "[i].value"))

I have tried the folowing...

code+"totalamount"=(eval("document.Order." +code+ "[i].value"))

Jona
05-09-2003, 08:53 AM
Use this:

code+=eval("document.Order." +code+ "[i].value"));