jquery - Can I declare and call functions in javascript like in php -


can done? i'm coming form php , trying optimize jquery code i'm writing validation , i'm repeating self lot.

this function:

function errorhint(e,hint){          if(e.parent().hasclass("has-success")){             return false;         }else{             e.addclass('form-control');             e.parent().addclass('has-error');             e.parent().append('<span id="glyph-error" class="glyphicon glyphicon-remove form-control-feedback"></span>');             e.parent().append('<div class="error-hint-empty alert alert alert-warning"><span class="warning-glyph glyphicon glyphicon-warning-sign"></span>'+hint+'</div>');             if(e.parent().hasclass("has-success")){                             e.parent().removeclass("has-success");                             e.parent().find('span#glyph-ok').remove();             }         }      } 

and call this:

if($('#username').length){     if(testuser == 0 && $('#username').val() !=='' && $('#username').val() !== null){         hint = 'that user exists!';         errorhint($("#username"),hint);         formvalid = false;     } } 

your function should be:

function alert (text){     alert(text); }  $(form).on("submit", function() {     var text = 'this alert text';             alert(text); }); 

this because there ambiguous call between javascript's native alert , defined alert. changed function name alert, should work fine.

just side thought..

why doing this?

you can use alert(text).

i don't think doing called code optimization. called code lengthification ;)

my advice if aren't doing inside alert() except again alerting, should better off using native alert('some text');


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -