#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

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


    hi,

    my code currently use a form to input the last part of a simple URL-link.

    http://domain.com/_ _ _ _ _ the Var is fetch from the form input (work fine)

    but if the form is empty...welll the link return an error and i wish to add a if/else statment just so that if the form is left empty i can have a dummy VAR insterted anyways and avoid the error404 that i currently get.

    Code:
    <script> 	function loopLookup (form) { 	var initials = form.initials.value; 	window.open("http://domain.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 know it's easy i am just really bad at this stuff

    Thanks
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,009
    Rep Power
    2791
    Welcome to DevShed, mariolemy.

    Try the the following.
    Javascript Code:
    var initials = (form.initials.value) ? form.initials.value : 'Default Value';


    Next time use [highlight=Javascript[color=black]][/color] CODE [/highlight], or when using the CODE tags, ensure that the text you are pasting does not have unusual formating, by first pasting it into a text editor.

    When posting an X/HTML / Javascript issue, do not post the unparsed server-side code; post the output. So no PHP next time or it's just guesswork for us.

    Cheers.

    [Additional] I have used the ternary / conditional operator for this.
    Last edited by Winters; May 30th, 2013 at 05:11 AM.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    Awesome, clear, efficient and educative (posting the source is cool!)



    You're the man!

IMN logo majestic logo threadwatch logo seochat tools logo