/    Sign up×
Community /Pin to ProfileBookmark

Simple JavaScript issue that is making my head ache!

I have simple script designed to check if

[LIST=1]

  • [*]

    If a div1 is visible (display : none or block)


  • [*]

    if a textfield has any content


  • [/LIST]

    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>[/CODE]

    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>

    [/CODE]

    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. ([I]it’s been a long day and it’s not getting any shorter![/I])

    to post a comment
    JavaScript

    8 Comments(s)

    Copy linkTweet thisAlerts:
    @Logic_AliJun 19.2013 — There's no code to check the content of the field.
    Copy linkTweet thisAlerts:
    @iainjsimonsauthorJun 19.2013 — <script type="text/javascript">

    function toggle_visibility(id, [B][U]tf[/U][/B]) {

    var e = document.getElementById(id);

    [B][U]var f = document.getElementById(tf)[/U][/B] ;

    if(e.style.display == 'none' && [U][B]f.value != ''[/B][/U])

    e.style.display = 'block';

    else

    e.style.display = 'none';

    }

    </script>

    Does that not check the value field?
    Copy linkTweet thisAlerts:
    @PadonakJun 19.2013 — <i>
    </i>function toggle_visibility(id,tf){
    var e=document.getElementById(id),f=document.getElementById(tf);
    e.style.display=(f.value!=='')?'block':'none';
    }
    Copy linkTweet thisAlerts:
    @007JulienJun 19.2013 — An other problem may occur.

    If the divs do not have inline style attribute, myDiv.style.display is (at first) [I]'undefined'[/I] !

    Then the use of Peter-Paul Koch function getStyle() can be useful
    Copy linkTweet thisAlerts:
    @iainjsimonsauthorJun 19.2013 — <i>
    </i>function toggle_visibility(id,tf){
    var e=document.getElementById(id),f=document.getElementById(tf);
    e.style.display=(f.value!=='')?'block':'none';
    }
    [/QUOTE]


    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';[/CODE]
    Copy linkTweet thisAlerts:
    @PadonakJun 19.2013 — why don't you try it instead of asking? ))
    Copy linkTweet thisAlerts:
    @iainjsimonsauthorJun 20.2013 — why don't you try it instead of asking? ))[/QUOTE]

    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.
    Copy linkTweet thisAlerts:
    @PadonakJun 20.2013 — 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.
    ×

    Success!

    Help @iainjsimons spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.30,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...