Merge multiple javascript functions into one -
I have these two JavaScript functions and wish to "merge" them in one. I will combine some of these and later converting functions and keep it simple and cleaner which will repeat the function.
& lt ;! - Convert to PD - & gt; & Lt; Script type = "text / javascript" & gt; Function init () {document.getElementById ('kg'). Onmouseup = function () {ifNaN (this.value)) {Warning ('number only !!'); . Document.getElementById ('kg') value = ''; . Document.getElementById ('PND') value = ''; Return; } Document.getElementById ('pnd'). Value = (this.value * 2.2046) .tfixed (1); }} If (window.addEventListener) {window.addEventListener ('load', init, false); } Else {if (window.attachEvent) {window.attachEvent ('onload', init); }} & Lt; / Script & gt; & Lt ;! - Convert feet to feet - & gt; & Lt; Script type = "text / javascript" & gt; Start function () {document.getElementById ('hauteur_cm'). Onmouseup = function () {ifNaN (this.value)) {Warning ('number only !!'); . Document.getElementById ('hauteur_cm') value = ''; Document.getElementById ('hauteur_pieds') value = ''. Return; } Document.getElementById ('hauteur_pieds'). Value = (this.value * 0.03280839895) .toFixed (1); }} If (window.addEventListener) {window.addEventListener ('load', start, wrong); } Else {if (window.attachEvent) {window.attachEvent ('onload', start); }} & Lt; / Script & gt;
Thanks for the help
Create an item in which one unit List of pairs of:
var unitpass = [[{from: 'kilogram', from: 'pd', factor: 2.2046}, {to: 'hauteur_cm', from: 'hauteur_pieds' , Factor: 0.03280839895}, / * ... * /];
Then you can write a function that handles all of them:
function init () {for (var i = 0; i & lt ; Unitpairs length; ++ i) {var pair = unipair [i]; Document.getElementById (pair.from) .onmouseup = function () {if (value)) {// ...} document.getElementById (pair.to) .value = (this.value * pair.factor ) .toFixed (1); } // <...}}
In addition to this, you should use the same style as jQuery, which is for you to do some compulsive work.
Comments
Post a Comment