Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding a simple if/else statment to a simple function/var

    Hi, i am so bad at programing i don't know how to do this task as easy as it is!!!


    I have that url-link that is filled by the form using a simple var... i want to add a "else" statement that would allow the url link to work even if the "var initials" is empty.


    Code:
    <script>
    	function loopLookup (form) {
    	var initials = form.initials.value;
    	window.open("http://names.whitepages.com/<?php the_title(); ?>/" + initials, "", 'width=775,height=550,scrollbars=yes');
    	}
    </script>
    <form name="" action="" method="">
    <input class="inputz" size="35" placeholder="<?php the_title(); ?>" type="text" disabled> + <input class="inputz" size="35" placeholder="Last Name" type="text" name="initials" value="" onkeydown="if(event.keyCode==13) loopLookup(this.form)"> = <input class="btn" type="button" name="button"  value="View Full Names USA Records" onclick="loopLookup(this.form)">
    </form>
    I hope the code is self explanatory as i am worst at explaining it, it feels.

    thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,892
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    I don't think tht I understand your question. If the form field initials is blank then the Javascript variable initials is also "" (blank). It is not a good idea to give the same name to an HTML element and a Javascript variable. Are you saying that you want some default value for the Javascript variable if the user does not enter his initials?

    Code:
    function loopLookup (form) {
    var initials = form.initials.value;
    if (initials.length <1) {initials = "MyDefaultValue"}
    What if the user enter $? or spaces as his initials?

    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 05-30-2013 at 11:42 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No clue what the reply meant...

    meanwhile i found the proper way instead of if/else statement

    var initials = (form.initials.value) ? form.initials.value : 'Default Value';


    thanks anyways


  •  

    Posting Permissions

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