Jump to content

onClick visibility issue


Gnub

Recommended Posts

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">

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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{
  }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.