SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot themissingelf's Avatar
    Join Date
    Nov 2001
    Location
    UK
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript check based on two elements...

    What is wrong with this code? I'm relatively new to Javascript, therefore, not very good at spotting why it does not work at times. Very grateful for any help.

    I have a checkbox that, when checked, has a value of "Yes". If checked and form field "_013_currsite" is blank I want a prompt box to appear asking for "...current web site". I then want to assign the value of the prompt field to "_013_currsite".

    In the checkbox input I have an onBlur event calling wwwPrompt.

    At the moment, the onBlur calls the function. The value entered in the prompt assigns to "_013_currsite"; however, what the function does not seem to spot is when there is an entry in "_013_currsite" already (in which case I do not want the prompt to appear).

    Any idea what's occuring here?

    PHP Code:
    function wwwPrompt(enquiryForm) {
        if (
    enquiryForm._016_review_site.value=="Yes" && enquiryForm._013_currsite.value=="");
    var 
    www prompt("Please enter your current web site address.",""); {
        
    enquiryForm._013_currsite.value=www; return false;
        }
    enquiryForm._017_site_hosting.focus(); return true;


  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is always true:

    enquiryForm._016_review_site.value=="Yes"

    This can be true or false:

    enquiryForm._016_review_site.checked

  3. #3
    SitePoint Zealot themissingelf's Avatar
    Join Date
    Nov 2001
    Location
    UK
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks jofa, however, I changed the script to:

    PHP Code:
    function wwwPrompt(enquiryForm) {
        if (
    enquiryForm._016_review_site.checked && enquiryForm._013_currsite.value=="");
    var 
    www prompt("Please enter your current web site address.",""); {
        
    enquiryForm._013_currsite.value=www; return false;
        }
    enquiryForm._017_site_hosting.focus(); return true;

    ...but the prompt box appears regardless of whether the box is checked or not (i.e. if I use tab to go through the fields on the form, as soon as _016_review_site onBlur, the prompt appears).

    Not sure where to go from here. Any further thoughts?

  4. #4
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remove the semicolon after if(...)

    Code:
    function wwwPrompt(enquiryForm) 
    {
      if(enquiryForm._016_review_site.checked && enquiryForm._013_currsite.value=="")
      {
        var www = prompt("Please enter your current web site address.",""); 
        enquiryForm._013_currsite.value=www; 
        return false;
      }
      enquiryForm._017_site_hosting.focus(); 
      return true;
    }

  5. #5
    SitePoint Zealot themissingelf's Avatar
    Join Date
    Nov 2001
    Location
    UK
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent, thank you.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •