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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:44:22+00:00 2026-06-04T05:44:22+00:00

How can I make this fiddle work: http://jsfiddle.net/gAHwW/ function $escape(string) { return string.replace(/\\(\[|\]\\)/g,’\\\\$1′); }

  • 0

How can I make this fiddle work:
http://jsfiddle.net/gAHwW/

function $escape(string) { 
    return string.replace(/\\(\[|\]\\)/g,'\\\\$1');
}

$(function() {
    $('input[type="button"]').click(function() {
        alert($escape( $(this).attr('id') )); // to show you what the escape does

        $('#' + $(this).attr('id')).hide(); // doesn't work
        $('#' + $escape( $(this).attr('id') )).hide(); // doesn't work
        $('#alsosquare[]').hide(); // doesn't work

        //$(this).hide(); // works
        //$('#alsosquare\\[\\]').hide(); // works
    });
});​

I need to select elements by their name/id dynamically, and their names/ids can have square brackets.

Thanks!

  • 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-04T05:44:24+00:00Added an answer on June 4, 2026 at 5:44 am

    You simply have to double escape(\\) the brackets.

    Here’s a demo: http://jsfiddle.net/k3YyX/


    Here’s a quote from the jQuery docs:

    If you wish to use any of the meta-characters (such as !”#$%&'()*+,./:;<=>?@[\]^`{|}~) as a literal part of a name, you must escape the character with two backslashes: \\. For example, if you have an element with id=”foo.bar”, you can use the selector $(“#foo\\.bar”).


    Update:

    Here’s your fiddle, in working condition: http://jsfiddle.net/gAHwW/1/

    All I did was replace '\\\\$1' with this '\\$1' in your $escape function.

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

Sidebar

Related Questions

In this fiddle http://jsfiddle.net/SySRb/105/ , function init(); successfully checks for the existence of an
Please take a look at this fiddle: http://jsfiddle.net/hughbe/QYGcq/ As you can see, the label
Made a fiddle for this: http://jsfiddle.net/terjeto/MN4FJ/ My problem is that dragleave fires when you
I have the following code: http://jsfiddle.net/maniator/vTjW8/ var createChartTemplate = function() { return { chart:
Please have a look at this fiddle: http://jsfiddle.net/mrmartineau/53fkV/embedded/result/ The intended outcome is that when
How i can make this condition en VB.NET If e.KeyCode = [a-zA-Z0-9] Then 'Blah
How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,
how can I make this work? $current_id=5; SELECT Id FROM table1 WHERE Output='yes' and
I can't seem to make Perl's flock work. I'm locking a file, checking return
How can I make this input transparent? <input type=text class=foo> I've tried this but

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.