PDA

Click to See Complete Forum and Search --> : Javascript help needed!!


MarcelB
Mar 13th, 2001, 05:04 AM
Hi everyone!

I have an input page on which I have a few selectboxes, each with the same name, but a different value. The selectboxes are placed on the page using an include file. After placing the boxes, I want to make a few of the options checked, but since they are already in my page, I need to use Javascript for that.

I have tried different ways, but none has worked yet. Any ideas?

I have the value of the option I want to have checked in a variable called TheseNeedToBeChecked. These are some of the ways I tried:

=====================================

var CheckThisOne = <%= TheseNeedToBeChecked %>;

myform.hobbies(CheckThisOne).selected = true;
myform.hobbies[CheckThisOne].selected = true;
myform.hobbies(CheckThisOne).selected;
myform.hobbies[CheckThisOne].selected;

myform.hobbies(CheckThisOne).checked = true;
myform.hobbies[CheckThisOne].checked = true;
myform.hobbies(CheckThisOne).checked;
myform.hobbies[CheckThisOne].checked;

=====================================

Any help would be highly appreciated. :)

Marcel

Active
Mar 13th, 2001, 05:29 AM
Did you mean you want some of the options in the
Select box Selected by default ?

Mar 13th, 2001, 05:46 AM
yes. :)

Mark Sreeves
Mar 13th, 2001, 07:47 AM
can you post/mail what you've got so far so I don't have to code an example from scratch?

Mar 13th, 2001, 08:04 AM
What do you mean "what I got"?

I have all the code, I only need the one line (I hope it's one line anyway) to get the checkbox checked. And when I say checked, I mean so that the "V" is in it, indicating the value is "on".

So the code I have so far is like this:
(the value "Hobbies" in the recordset UserData contains the indexes of the checkboxes I want to have checked, seperated by ";")

===================================

dim strHobbies
strHobbies = UserData("Hobbies")
dim intInPos
intInPos = instr(strHobbies, ";")
dim strHobbyName
dim intIntHobbyID
do while intInPos <> 0
intIntHobbyID = cint(left(strHobbies, intInPos - 1))%>

<script language="javascript">
var HobbyID2Check = <%= intIntHobbyID %>;
myform.hobbies[HobbyID2Check].checked = true;
</script>

<%strHobbies = right(strHobbies, len(strHobbies) - intInPos)
intInPos = instr(strHobbies, ";")
loop%>

=====================================

Hmm.. I see now that I will probably also need to process the last value (when there is just the value left in the string, and no more semi-colons)

Hope you can help. Thanx in advance. :)

Marcel

Mar 13th, 2001, 08:22 AM
For checking a Checkbox you can use this syntax.

// JavaScript, Setting the checkbox on
Checkbox.checked = 1;

Mark Sreeves
Mar 13th, 2001, 08:22 AM
OK, I can't see where on your page the include for writing the check boxes is so I've assumed it is between the body tags.
Here's what I've got anyway.

ch.js


document.write("<form name=frm1>");

document.write("<input name=hobbies type=checkbox> zxdfgh</input><BR>");
document.write("<input name=hobbies type=checkbox> hgfdhsdfh</input><BR>");
document.write("<input name=hobbies type=checkbox> ryqreyr</input><BR>");
document.write("<input name=hobbies type=checkbox> cxzvbczvxb</input><BR>");

document.write("</form>");



ch.asp

<%@ Language=VBScript %>
<html>

<head>
<% dim x

x=2

%>

<script LANGUAGE="JavaScript">
function init(){

document.frm1.hobbies[<%=x%>].checked= true

}
</SCRIPT>

</head>

<body onload="init()">

<script language="JavaScript"
SRC="ch.js">
</SCRIPT>

</body>
</html>




is this any help?

Mar 13th, 2001, 09:31 AM
Well, not really, coz that wasn't really what I meant..

But for some reason, now all of a sudden, it seems to work (after I realised it used the index instead of the ID of the checkbox to check). I also adjusted the code so it takes care of that last item aswell.

So it works now.
Thanx, guys.. :)

Mar 13th, 2001, 09:31 AM
Well, not really, coz that wasn't really what I meant..

But for some reason, now all of a sudden, it seems to work (after I realised it used the index instead of the ID of the checkbox to check). I also adjusted the code so it takes care of that last item aswell.

So it works now.
Thanx, guys.. :)