•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 426,901 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,376 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1132 | Replies: 8 | Solved
![]() |
| |
•
•
Join Date: Apr 2008
Posts: 17
Reputation:
Rep Power: 1
Solved Threads: 0
Hi everyone, I am so new to javascript and it is really giving me a micrain. It looks easy but tend to be hard. Atleast for me since I am a beginner.
I need help please. I need to make work with the element getElementById
I have tried numerous way for days and nothing is working. All I am trying to do is when onchange the user go on the option credit card, the credit card information box shows. This is what I have in the script tag. Can some please tell me what am I doing wrong and what should I have done?
below is in the body
I need help please. I need to make work with the element getElementById
I have tried numerous way for days and nothing is working. All I am trying to do is when onchange the user go on the option credit card, the credit card information box shows. This is what I have in the script tag. Can some please tell me what am I doing wrong and what should I have done?
JavaScript Syntax (Toggle Plain Text)
function CheckForCreditCard() if document.getElementById("divCreditCardInfo").options3].selected == true) { CreditCardInfo.style.visibility = "visible"; }
below is in the body
html Syntax (Toggle Plain Text)
<select name="selPayment" onchange="CheckForCreditCard();"> <option value="Cash">Cash</option> <option value="Check">Check</option> <option value="Credit Card" selected="selected">Credit Card</option> </select> <div id="CreditCardInfo"> <br /> Card Number: <input name="selCreditCardInfo" size="20" type="text" /> Month: <input type="text" name="txtMonth" size="4" /> Year: <input name="txtYear" size="4" type="text" /> </div>
Last edited by peter_budo : May 16th, 2008 at 6:50 am. Reason: The code tags encapsulate your code, not to be replace by your code ;)
•
•
Join Date: Aug 2006
Location: Noida, India
Posts: 158
Reputation:
Rep Power: 3
Solved Threads: 17
The function should be like this :
You should first get a refrence for any document element before setting any properties. I.E done by:
function CheckForCreditCard()
if document.getElementById("divCreditCardInfo").options3.selected == true)
{
var CreditCardInfo = document.getElementById("CreditCardInfo");
CreditCardInfo.style.visibility = "visible";
}You should first get a refrence for any document element before setting any properties. I.E done by:
var CreditCardInfo = document.getElementById("CreditCardInfo");•
•
Join Date: Apr 2008
Posts: 17
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
The function should be like this :
function CheckForCreditCard() if document.getElementById("divCreditCardInfo").options3.selected == true) { var CreditCardInfo = document.getElementById("CreditCardInfo"); CreditCardInfo.style.visibility = "visible"; }
You should first get a refrence for any document element before setting any properties. I.E done by:
var CreditCardInfo = document.getElementById("CreditCardInfo");
Okay I understand why you added the variable line CreditCardInfo. You have it so the variable I have in the heading of my javascript is called onto and identify with the function of the credit card info to show if it is set to visible.
I notice that you did not put the 3 in [] after options, but I did; because dealing with selections or check numbers they star with a [0] and should be in boxes for them to work.
One thing I don't understand is that everything look good but why is it still not working. Why is it when I go on credit card that the credit card information is not displaying.
I told it
CreditCArdInfo.style.visibility = "visible";
•
•
Join Date: Aug 2006
Location: Noida, India
Posts: 158
Reputation:
Rep Power: 3
Solved Threads: 17
Then the if-condition should be:
because you have only three options: option[0], option[1] and option[2]. There is no option[3] option.
if (document.getElementById("divCreditCardInfo").options[2].selected == true)because you have only three options: option[0], option[1] and option[2]. There is no option[3] option.
Last edited by Luckychap : May 16th, 2008 at 3:25 am.
•
•
Join Date: Aug 2006
Location: Noida, India
Posts: 158
Reputation:
Rep Power: 3
Solved Threads: 17
if its taking too much time, here is your whole code:
Javascript function:
here is the HTML code
Check this out.
Javascript function:
function CheckForCreditCard(el) {
if (el.options[2].selected == true)
{
var CreditCardInfo = document.getElementById("cci_div");
CreditCardInfo.style.visibility = "visible";
}
}here is the HTML code
<select name="selPayment" onchange="CheckForCreditCard(this);"> <option value="Cash">Cash</option> <option value="Check">Check</option> <option value="Credit Card" selected="selected">Credit Card</option> </select> <div id="cci_div"> <br /> Card Number: <input name="selCreditCardInfo" size="20" type="text" /> Month: <input type="text" name="txtMonth" size="4" /> Year: <input name="txtYear" size="4" type="text" /> </div>
Check this out.
Last edited by Luckychap : May 16th, 2008 at 4:22 am.
•
•
Join Date: Apr 2008
Posts: 17
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
The function should be like this :
function CheckForCreditCard() if document.getElementById("divCreditCardInfo").options3.selected == true) { var CreditCardInfo = document.getElementById("CreditCardInfo"); CreditCardInfo.style.visibility = "visible"; }
You should first get a refrence for any document element before setting any properties. I.E done by:
var CreditCardInfo = document.getElementById("CreditCardInfo");
Thank you very much Luckychap. You have really gave me a greater understanding of dealing with getElementById. My book, "The book of Javascript 2nd Edition", only touch on it and did not explain it in detail to understanding as you did. I will take this to the grave with me. I Thank you very much for your time and your patience. Have a wonderful weekend!
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Validating radio button selection for login redirect
- Next Thread: A new simply way to make a image slider- Javascript Code


Hybrid Mode