User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Apr 2008
Posts: 17
Reputation: vanessia_1999 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
vanessia_1999 vanessia_1999 is offline Offline
Newbie Poster

Pull down menue using getElemntById --Help Please

  #1  
May 16th, 2008
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?

  1. function CheckForCreditCard()
  2. if document.getElementById("divCreditCardInfo").options3].selected == true)
  3. {
  4. CreditCardInfo.style.visibility = "visible";
  5. }

below is in the body

  1. <select name="selPayment" onchange="CheckForCreditCard();">
  2. <option value="Cash">Cash</option>
  3. <option value="Check">Check</option>
  4. <option value="Credit Card" selected="selected">Credit Card</option>
  5. </select>
  6.  
  7.  
  8. <div id="CreditCardInfo"> <br />
  9. Card Number:
  10. <input name="selCreditCardInfo" size="20" type="text" />
  11. &nbsp;&nbsp; Month:
  12. <input type="text" name="txtMonth" size="4" />
  13. &nbsp;&nbsp;Year:
  14. <input name="txtYear" size="4" type="text" />
  15. </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 ;)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 158
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 17
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: Pull down menue using getElemntById --Help Please

  #2  
May 16th, 2008
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");
Reply With Quote  
Join Date: Apr 2008
Posts: 17
Reputation: vanessia_1999 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
vanessia_1999 vanessia_1999 is offline Offline
Newbie Poster

Re: Pull down menue using getElemntById --Help Please

  #3  
May 16th, 2008
Originally Posted by Luckychap View Post
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";
so why is it not doing what it is suppose too. the function is in the head between javascript
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 158
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 17
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: Pull down menue using getElemntById --Help Please

  #4  
May 16th, 2008
Then the if-condition should be:

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.
Reply With Quote  
Join Date: Apr 2008
Posts: 17
Reputation: vanessia_1999 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
vanessia_1999 vanessia_1999 is offline Offline
Newbie Poster

Re: Pull down menue using getElemntById --Help Please

  #5  
May 16th, 2008
I am like so dumb. I know that, but over look it. I javascript really frustate me. When everything look good it still does not work.

I do thank you for responding.
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 158
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 17
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: Pull down menue using getElemntById --Help Please

  #6  
May 16th, 2008
if its taking too much time, here is your whole code:

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" />
&nbsp;&nbsp; Month:
<input type="text" name="txtMonth" size="4" />
&nbsp;&nbsp;Year:
<input name="txtYear" size="4" type="text" />
</div>

Check this out.
Last edited by Luckychap : May 16th, 2008 at 4:22 am.
Reply With Quote  
Join Date: Apr 2008
Posts: 17
Reputation: vanessia_1999 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
vanessia_1999 vanessia_1999 is offline Offline
Newbie Poster

Re: Pull down menue using getElemntById --Help Please

  #7  
May 16th, 2008
Originally Posted by Luckychap View Post
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!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 11:07 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC