www.webdeveloper.com
Results 1 to 9 of 9

Thread: Simple JavaScript issue that is making my head ache!

  1. #1
    Join Date
    Jul 2011
    Posts
    8

    Simple JavaScript issue that is making my head ache!

    I have simple script designed to check if
    1. If a div1 is visible (display : none or block)
    2. if a textfield has any content


    The page is designed to show a div section containing 4 text fields that are repeated on the page depending on the above conditions, so if a qty is entered in the first section if would check if the second div is set to display : none and if it is change this to block so it is viewable, it then also checks to see see if they first quantity field is not blank so it does not toggle the form back to none on entering a second figure.

    Code:
    <script type="text/javascript">
    function toggle_visibility(id, tf) {
    var e = document.getElementById(id);
    var f = document.getElementById(tf) ;
    if(e.style.display == 'none' && f.value != '')
    e.style.display = 'block';
    else
    e.style.display = 'none';
    }
    </script>
    HTML

    Code:
    <input name="QUANTITY1" type="text" class="txtfield"  id="QUANTITY1" title="" onkeyup="toggle_visibility('Part2','QUANTITY1');" value="" size="4" maxlength="10"/>
    
    <div id="Part2" style="display: none;">
    Part2 <input class="txtfield" name="PART_NO2" type="text" id="PART_NO2" title="" value="" size="16" />
    Size  <input class="txtfield" name="SIZE2" type="text" id="SIZE2" title="" value="" size="8" />
    Qty <input class="txtfield" name="QUANTITY2" type="text" id="QUANTITY2" title="" value="" size="4" maxlength="10" />
    </div>
    But for some reason the second part of the JavaScript if condition is being ignored and it just toggle the display with each key entry, Have I missed something simple?

    Thanks for looking. (it's been a long day and it's not getting any shorter!)

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    There's no code to check the content of the field.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Jul 2011
    Posts
    8
    <script type="text/javascript">
    function toggle_visibility(id, tf) {
    var e = document.getElementById(id);
    var f = document.getElementById(tf) ;
    if(e.style.display == 'none' && f.value != '')
    e.style.display = 'block';
    else
    e.style.display = 'none';
    }
    </script>

    Does that not check the value field?

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    Code:
    function toggle_visibility(id,tf){
    var e=document.getElementById(id),f=document.getElementById(tf);
    e.style.display=(f.value!=='')?'block':'none';
    }
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,266
    An other problem may occur.
    If the divs do not have inline style attribute, myDiv.style.display is (at first) 'undefined' !
    Then the use of Peter-Paul Koch function getStyle() can be useful

  6. #6
    Join Date
    Jul 2011
    Posts
    8
    Quote Originally Posted by Padonak View Post
    Code:
    function toggle_visibility(id,tf){
    var e=document.getElementById(id),f=document.getElementById(tf);
    e.style.display=(f.value!=='')?'block':'none';
    }
    Padonak, you are a star, works perfect.

    Still don't fully understand the short hand way of doing it but would it be possible to add further conditions such as

    Code:
    e.style.display=(f.value!==''&&g.value!=="")?'block':'none';

  7. #7
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    why don't you try it instead of asking? ))
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  8. #8
    Join Date
    Jul 2011
    Posts
    8
    Quote Originally Posted by Padonak View Post
    why don't you try it instead of asking? ))
    Is it normal on this forum to be rude and obnoxious?

    As I said I do not fully understand JavaScript shorthand hence why I asked, Instead of just seeing an answer would it not make more sense to understand why & how first? As I have not seen the IF query written in such a way I would like to not know if there are any restrictions with such a method. A simple "yes it is fine" or "no due to etc. etc" this way I could continue to understand and learn.

    How many times have people on forums such this asked questions where they just want the answer or someone to write the code for them which ultimately results in the people replying to lose patience and tell them "why don't you just learn it?"

    This is not a rant but merely a request for a little manners and understanding that not all posters are just looking for the solution but also to learn.

  9. #9
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    dear iainjsimons, i didn't mean to offend you. as you could notice, i am from Russia and this is not my native language. i am learning English by myself here and there from people, mostly at this forum, and perhaps sometimes i don't feel some language nuances. you can easily understand what i am talking about if you try to describe your coding problems in russian and then read the answer written in russian.

    anyway, i wrote just what i thought. this is the way i learned and keep learning. testing that double condition could take you a couple of minutes but you didn't just check to see how it works and decided to wait for the answer. why? who am i to take my answers as established truth? you are teaching me manners but let me tell you how your working code was created. usually i do not write any answer without testing it locally on my comp. 99% of people who ask for a help usually provide only a piece of code, so i have to manually add the missing parts of the markup to make a page every time. by the way, the same story about your code. did you see in my post anything like "where is the missing markup?! where are your manners and understanding?!"?

    so, do not be so techy, and try to accept people as they are, especially when they help you.
    Last edited by Padonak; 06-20-2013 at 07:29 AM.
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles