jquery - Reducing code redundancy in a series of javascript function expressions -
i have long series of javascript functions invoked 'onclick' on function name in html.
corporate number <input type="text" autofocus id="corp_text"> <button onclick="getcorpnum(this.form)">save</button><br><br> llc number <input type="text" autofocus id="llc_text"> <button onclick="getllcnum(this.form)">save</button><br><br> type of nonprofit <input type="text" autofocus id="np_text"> <button onclick="getnonprofit(this.form)">save</button><br><br> other description <input type="text" autofocus id="od_text"> <button onclick="getdesc(this.form)">save</button><br><br>
in javascript functions similar in form:
/** * corporation number text field data. */ var getcorpnum = function() { var corp_number = []; corp_number=document.getelementbyid("corp_text").value; if (corp_number.length > 0) createjsonobj("corp", corp_number); } /** * llc number text field data. */ var getllcnum = function() { var llc_number = []; llc_number = document.getelementbyid("llc_text").value; if (llc_number.length > 0) createjsonobj("llc", llc_number); } /** * type of non-profit text field. */ var getnonprofit=function() { var np_number = []; np_number = document.getelementbyid("np_text").value; if (np_number.length > 0) createjsonobj("np", np_number); } /** * other description text */ var getdesc=function() { var getdesc=[]; getdesc = document.getelementbyid("od_text").value; if (getdesc.length > 0) createjsonobj("other_desc", getdesc); }
the issue have other functions these have same form , i'd reduce amount of code. generic capture form of code. difficult me because onclicks need name of of each function object.
you pass in different parts.
corporate number <input type="text" autofocus id="corp_text"> <button onclick="getthing('corp_text', 'corp')">save</button><br><br>
and use variables in more generic function:
var getthing = function(id, name) { var values=document.getelementbyid(id).value; if (values.length > 0) createjsonobj(name, values); }
but . . . maybe more descriptive "getthing" :)
Comments
Post a Comment