Gnub Posted July 9, 2007 Share Posted July 9, 2007 Hi guys, i will put my hand up and say im pretty useless in Javascript, but i must use it. Im *trying* to get an onClick event to change the visibility of a textbox, when the selection equals a specific value. Im lost, and dont know what im doing wrong. Any help would be great. (code snippet is below) function creditcard(FormName) { IF (document.FormName.fCardType = 'Visa' || 'Mastercard') {document.FormName.fPprice.style.visibility="visible"} else {document.FormName.fPprice.style.visibility="hidden"} } <select id="fCardType" name="Card_Type" onChange="creditcard()" size="1"> <input id="fPprice" type="text" name="CCPrice" value="<%=CC%>" size="4"> Quote Link to comment https://forums.phpfreaks.com/topic/59102-onclick-visibility-issue/ Share on other sites More sharing options...
nogray Posted July 9, 2007 Share Posted July 9, 2007 change this IF (document.FormName.fCardType = 'Visa' || 'Mastercard') to if ((document.FormName.fCardType == 'Visa') || (document.FormName.fCardType == 'Mastercard')) Quote Link to comment https://forums.phpfreaks.com/topic/59102-onclick-visibility-issue/#findComment-293868 Share on other sites More sharing options...
Gnub Posted July 16, 2007 Author Share Posted July 16, 2007 sorry for the delay... but that solved a bug, but now i get a 'document.FormName.fPprice.style' is null or not an object Any idea what is not going right? all the JS code and form related info is on the first post. Quote Link to comment https://forums.phpfreaks.com/topic/59102-onclick-visibility-issue/#findComment-299478 Share on other sites More sharing options...
roopurt18 Posted July 16, 2007 Share Posted July 16, 2007 Think about this for a moment and see if it doesn't help you solve the problem: In order to be useful to a server-side script, a form field has to have a name; assigning IDs to HTML elements is only useful to a client side scripting language. If you look at your document.FormName.field statements, you are using the ID attribute. Try using the name attribute instead. To tie that back in with my original statement in this post, document.FormName.field is meant to access a control within a form. It would make sense to have a correlation between field and the actual element that is almost guaranteed to exist. Since all useful elements have a name, but not necessarily an ID, the name attribute was chosen. Quote Link to comment https://forums.phpfreaks.com/topic/59102-onclick-visibility-issue/#findComment-299911 Share on other sites More sharing options...
Gnub Posted July 19, 2007 Author Share Posted July 19, 2007 Ok, i've taken what you've said on board. So i looked over the script, replaced the FormName.field to the name and not the ID. Why it's taken me a while to reply, wanted to try anything i could think of that would fix it. As i said, im not that good in java, probably less than basic understanding of it's language. The error has changed now says that the Card_Type is null or not an object. (Card_Type, is a option list, so as far as im aware it exists.) Code now looks as below: function creditcard(FormName) { if ((document.FormName.Card_Type !== 'Visa') || (document.FormName.Card_Type !== 'Mastercard')) {document.FormName.CCPrice.style.visibility="hidden"} else {document.FormName.CCPrice.style.visibility="visible"} } <select id="fCardType" name="Card_Type" onChange="creditcard()" size="1"> <input id="fPprice" type="text" name="CCPrice" value="<%=CC%>" size="4"></td> I prefer not being given the answer, rather than help in solving it. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/59102-onclick-visibility-issue/#findComment-302220 Share on other sites More sharing options...
roopurt18 Posted July 19, 2007 Share Posted July 19, 2007 When all else fails, dump output to the screen and see if it's what you expect. In this case, try: alert(document.FormName.Card_Type); Make sure it says something about it being an object. If it's not, then it might just be easier to do something like: var ctype = document.getElementById("fCardType"); if(ctype.options[ctype.selectedIndex].value != "Visa" || ctype.options[ctype.selectedIndex].value != "Mastercard"){ }else{ } Quote Link to comment https://forums.phpfreaks.com/topic/59102-onclick-visibility-issue/#findComment-302495 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.