Note that there are some explanatory texts on larger screens.

plurals
  1. POtargeting form fields at once(JavaScript shorthand kinda?)
    primarykey
    data
    text
    <p>So long story short, i have some forms (2) of them that, need different types of validation...for the sake of argument, lets say i want to check for empties or "" values on all of the forms fields.</p> <p>if i have for example 3 fields, how can i check for empties in those 3 fields at the same time without writing code for all of them...</p> <p>what i mean is...for example:</p> <p>heres some sample code:</p> <p>JS: </p> <pre><code>var test = document.getElementById("loginUsername");//this is the username field// test.onfocus = removeVal; test.onblur = inserDefVal; function removeVal(){ if(test.value == "Enter your Username"){ test.value = ""; } } function inserDefVal(){ if(test.value == ""){ test.value = "Enter your Username"; } } </code></pre> <p>now although i have those 2 functions to check for field values, if i have 3 fields, that means id have to have a list of</p> <pre><code>test.onfocus = removeVal; test.onblur = inserDefVal; test2.onfocus = removeVal; test2.onblur = inserDefVal; test2.onfocus = removeVal; test2.onblur = inserDefVal; </code></pre> <p>now imagine all other fields in a form that i want to check for empties...ill have a SUPER long list lol..</p> <p>so my quesiton is, is there a way to maybe, put all these into one... and or maybe</p> <p>like a general function/or action that says something like </p> <p>(logic i mean) here comes pseudo code --</p> <p><code>if(this.value =="") do xyz</code>....and "this" being the field im mousing over. </p> <p>or if say, i was to somehow..for example, put all the button names/variable names in an Array, and say its called ArraOfButtons, and then</p> <pre><code>ArrayOfButtons.onmouseover/out = function..... </code></pre> <p>I hope im not too confusing...im just looking for a way to write better,faster cleaner code because as you can see above, if i have those test1/2/3 to just check for empties, then imagine checking for dates and other values....the page would be SUPER long.</p> <p>in short, im looking for like a CSS shorthand kinda thing where, instead of one writing</p> <pre><code>margin-top:10px / margin-bottom:10px / margin-left/right:10px </code></pre> <p>one would just do</p> <p>margin:10px;</p> <p>Any tips/links/info i gladly appreciate.</p> <p>Thanks in advanced.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload