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 7400671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:17:00+00:00 2026-05-29T04:17:00+00:00

i wrote jquery custom fn extension script to remove the given element in an

  • 0

i wrote jquery custom fn extension script to remove the given element in an array,
but it works unexpected way so i want to figure out how to make it do reference manipulation without using return reference way.

$.fn.removeElement=function(e){
    var l=this.length;
    while(l--){
        if(this[l]==e){
            console.log(this);
            this.splice(l,1);
            console.log(this);
            break;
}   }   }
var x=['a','b','c','d','e'];
console.log(x);
$(x).removeElement('c');
console.log(x);

result is like the below,

> ["a", "b", "c", "d", "e"]
> ["a", "b", "d", "e"]
> ["a", "b", "d", "e"]
> ["a", "b", "c", "d", "e"] //???

i expect [“a”, “b”, “d”, “e”] in the forth line.

p.s. i don’t want ‘return stuff’..

  • 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-05-29T04:17:01+00:00Added an answer on May 29, 2026 at 4:17 am

    For one, this line of code:

    if('object'==typeof(e))
    

    is like this in your code example:

    if('object'==typeof(3))
    

    which is

    if('object'=='number')
    

    which will never match.

    I would also ask why you’re trying to wrap a normal array in a jQuery object. It would be better to just use a method that operates on an array.

    As a global function, you could do it like this:

    function removeArrayElement(array, itemToRemove) {
        for (var i = 0; i < array.length; i++) {
            if (array[i] === itemToRemove) {
                array.splice(i, 1);
                break;
            }
        }
    }
    
    var x = ['a','b','c','d','e'];
    removeArrayElement(x, 'c');
    

    As a global method on the jQuery object, you could do it like this:

    $.removeArrayElement = function(array, itemToRemove) {
        for (var i = 0; i < array.length; i++) {
            if (array[i] === itemToRemove) {
                array.splice(i, 1);
                break;
            }
        }
    }
    
    var x = ['a','b','c','d','e'];
    $.removeArrayElement(x, 'c');
    

    As a method on the Array object, you could do it like this:

    Array.prototype.removeElement = function(itemToRemove) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] === itemToRemove) {
                this.splice(i, 1);
                break;
            }
        }
    }
    
    var x = ['a','b','c','d','e'];
    x.removeElement('c');
    

    Working example of the prototype method here: http://jsfiddle.net/jfriend00/tEXWH/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote my custom jquery validation script to validate the fields in the form
before this I wrote all jquery-code into main fail. Now I want to move
$(function(){ $('a').each(function(){ var x=this.href; this.href=www.somesitename.com/filter+this.href; }); }); i wrote the above jQuery script to
i wrote a jquery function and now i want to use it as a
I wrote a custom validator using MVC DataAnnotations and the jQuery unobtrusive javascript library.
I'm writing a custom method for a jQuery plugin: jQuery.validator.addMethod(alphanumeric, function(value, element) { return
I am using JQuery UI script which contains all the modules, including a custom
I wrote few custom view helpers but I have a little trouble using them.
I am using jQuery's UI dialogs and I want to add a custom method.
I'm using the jQuery validation plugin and I want make use of a custom

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.