1

I've got the following code

if(!$rta_cl_fn.length > 0){
    formSubmit = false;
    $('#div_cl_fn').addClass('has-error');
}else{
    $('#div_cl_fn').removeClass('has-error');
}

if(!$rta_cl_ln.length > 0){
    formSubmit = false;
    $('#div_cl_ln').addClass('has-error');
}else{
    $('#div_cl_ln').removeClass('has-error');
}

if(!$rta_cl_mn.length > 0){
    formSubmit = false;
    $('#div_cl_mn').addClass('has-error');
}else{
    $('#div_cl_mn').removeClass('has-error');
}

and there's loads more i just wanted to make a function so i just call the function rather than using if statements again and again

I've asked many a times but no one seems to understand what i want... i can show you what i'm looking in php format below

public function functionName($var1, $var2){

    $formSubmit = true;

    if(!$var1.length > 0){
        formSubmit = false;
        $var2.addClass('has-error');
    }else{
        $var2.removeClass('has-error');
    }

    return formSubmit;
}

if you need more clarity please ask

html is below

<div class="row">
    <div class="col-md-3">
        <div class="form-group">
            <label class="control-label">First Name <span class="required" aria-required="true" > * </span></label>
            <input type="text" name="rta_cl_fn" id="rta_cl_fn" class="form-control input-sm">
        </div>
    </div>
    <div class="col-md-5">
        <div class="form-group" id="div_cl_fn">
            <label class="control-label">Middle Name <span class="required" aria-required="true" > * </span></label>
            <input type="text" name="rta_cl_mn" id="rta_cl_mn" class="form-control input-sm">
        </div>
    </div>
    <div class="col-md-4">
        <div class="form-group" id="div_cl_ln">
            <label class="control-label">Last Name<span class="required" aria-required="true" > *</span></label>
            <input type="text" name="rta_cl_ln" id="rta_cl_ln" class="form-control input-sm">
            <span class="help-block"></span>
        </div>
    </div>
</div>

again my question is how to declare/create functions in jQuery and using them in the code.

Regards

4
  • just like as we do in javascript. define it anywahere and call it whenever needed. Commented Jun 20, 2014 at 10:33
  • did i say i know how to use javascript i dont know those two things.. miss vasisakh pc Commented Jun 20, 2014 at 10:35
  • what is $rta_cl_fn this variable for, can you please share that code also and share relevant html code. Commented Jun 20, 2014 at 10:37
  • so you want to make form with required fields? Commented Jun 20, 2014 at 10:54

6 Answers 6

2

Here is how you can create reusable functions using jQuery

(function($) {
    $.fn.functionName = function (var1, var2) {
            var formSubmit = true;

        if(!var1.length > 0){
            formSubmit = false;
            var2.addClass('has-error');
        }else{
            var2.removeClass('has-error');
        }

        return formSubmit;
        }
    };
  }(jQuery));

USAGE:

console.log($(".form-group").functionName('param1', 'param2'));

OR

a pretty much simpler way is by using a var to declare your function.

var myReusableFunction = function(var1, var2){
        var formSubmit = true;

        if(!var1.length > 0){
            formSubmit = false;
            var2.addClass('has-error');
        }else{
            var2.removeClass('has-error');
        }

        return formSubmit;
};

USAGE:

You can call this function similar to normal javascript functions

console.log(myReusableFunction('param1', 'param2'));

Check this tutorial

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

i'm getting the following error (Uncaught TypeError: undefined is not a function parties.js:7 isValidtf parties.js:7 (anonymous function) parties.js:28 m.event.dispatch jquery.min.js:3 r.handle)
@karthik You may want to see the last comment I posted under my answer.
1

Your PHP mockup, isn't really PHP code, but I think I get the point. You're having problems converting it to JS.

function isValid(test_obj, element){
  if(test_obj.length == 0){
    element.addClass('has-error');
    return false;
  } else {
    element.removeClass('has-error');
    return true;
  }
}

Then calling the function would look like this:

var form_submit = isValid($('#rta_cl_fn'), $('#div_cl_fn'));
if (form_submit) {
  form_submit = isValid($('#rta_cl_ln'), $('#div_cl_ln'));
  if (form_submit) {
    form_submit = isValid($('#rta_cl_mn'), $('#div_cl_mn'));
  }
}

if (form_submit) {
  //Submit form
} //else, an error along the way

5 Comments

thank you @Jeremy Millar This is what i want to know... Thanks once again
No problem. If this is answer which best helps, be sure to accept by checking the checkmark. If another answer comes along which is more helpful, then be sure to check that one. :)
when i use that code i get that error on consol (Uncaught TypeError: undefined is not a function parties.js:48 isValid parties.js:48 (anonymous function) parties.js:18 m.event.dispatch jquery.min.js:3 r.handle)
I would have to see the code. fiddle? When I execute it, no errors are thrown. Also, I am not using any anonymous functions.
No problem. I think you are a bit confused on accepting answers. You can only accept one. It is generally more considerate that if you have already accepted one, you do not unaccept to accept another without substantial reason. Feel free to restore this to karthik as that was the first-accepted answer. I'm glad we were both able to help.
1

Declare the function as follows in JS:

function functionName(var1, var2){

    var formSubmit = true;

    if(!var1.length > 0){
        formSubmit = false;
        var2.addClass('has-error');
    }else{
        var2.removeClass('has-error');
    }

    return formSubmit;
}

Use it anywhere in you JS:

functionName($('#rta_cl_fn'), $('#div_cl_fn'));

2 Comments

Thanks for you answer please note I dont know nothing about javascript i've learned some jquery.. therefore how would i use this in jquery scenario regards
Consider following working example on JSFiddle http://jsfiddle.net/sSeNN/1. It clarifies the idea with the functions I've made. Hopefully this is a working solution for you?
1
public function functionName (items, elem)
{
    var formSubmit = !items.length;
    elem.toggleClass('has-error', !formSubmit);
    return formSubmit;
}

Comments

1
$(document).ready(function({
    var myVariable = functionName(1,$("#myElement"));
    alert(myVariable)
});


function functionName(var1, var2){
    if(!var1.length > 0){
        var2.addClass('has-error');
        return false;
    }else{
        var2.removeClass('has-error');
        return true;
    }
}


You can use it

Comments

0

Your function looks like this in Javascript/jQuery, assuming the parameters are jQuery objects:

function functionName(var1, var2){
    if(!var1.length > 0){
        var2.addClass('has-error');
        return false;
    }else{
        var2.removeClass('has-error');
        return true;
    }
}

I removed the return variable because it isn't necessary within the function – you'd have to take a look at the return values of the function calls to test if any error occurred.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.