Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:34:37+00:00 2026-06-12T16:34:37+00:00

I am new to jQuery prototypes. I do this same code many times: $message

  • 0

I am new to jQuery prototypes. I do this same code many times:

   $message
        .removeClass()
        .addClass("message error")
        .show()
        .html('<li>' + json.Errors.join('</li><li>') + '</li>');

the variables here are the “error” and the html data. For success the addClass would be “message success”.

Is there a way I could combine these four into one function and then just call the one and pass it success/error and the data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:34:38+00:00Added an answer on June 12, 2026 at 4:34 pm

    Adding a jQuery function of your own is easy:

    $.fn.showErrors = function() {
      return this.removeClass().addClass("message error").show()
        .html('<li>' + json.Errors.join('<li>'));
    };
    

    Then:

    $message.showErrors();
    

    Note that you don’t really need the </li> closing tags.

    In genera, a jQuery function added like that as a property of $.fn should expect this to refer to the current jQuery object. The function should return that value so that the normal chaining behavior of jQuery will work. The one slightly confusing thing is that because this is alread a jQuery object, there’s no need to wrap it again — no need to write $(this) in other words.

    edit — sorry I’ve been away from the Internet for a bit (shocking!) That code assumes that “json” is just floating around. It probably should be a parameter:

    $.fn.showErrors = function(json) {
      return this.removeClass().addClass("message error").show()
        .html('<li>' + json.Errors.join('<li>'));
    };
    

    To call that:

    $message.showErrors(json);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im new to jquery and javascript.. Here is the code, this is a sample,
Is there a jQuery equivalent to this prototype code? Ajax.Responders.register({ onException: function(x,y) { if(y.message!=Syntax
Using jQuery UI 1.8rc3 combined with the new jquery.effects.fade.js code, I've been able to
In Prototype I can show a loading... image with this code: var myAjax =
Consider this: Math.__proto__.eps=1e-16; jQuery(document).ready(function() { var chart = new Highcharts.Chart({ //... }); }); Quite
What would be the jQuery equivalent to this Prototype code? If you don't think
I am new in createjs and jquery mobile. This may be a simple question,
I am new to prototype and finding it a lot more difficult than jquery.
When writing a new jQuery plugin is there a straightforward way of checking that
I've been trying to use the new jquery-ui, and all but the resizable function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.