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

  • Home
  • SEARCH
  • 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 6853207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:29:04+00:00 2026-05-27T01:29:04+00:00

Why selector works in first example, but fails in second one? See jsfiddle .

  • 0

Why selector works in first example, but fails in second one?
See jsfiddle.

<div id="hello[1][2]_world">&nbsp;</div>
<textarea id="console"></textarea>

<script>
   $(document).ready(function() {

      //first example
      $('#hello\\[1\\]\\[2\\]_world').html('01234'); //everything is OK

      //second example
      var iid = 'hello[1][2]_world';    
      iid = iid.replace(/[#;&,.+*~':"!^$[\]()=>|\/]/g, "\\\\$&");
      $('#console').val(iid); //see in textarea, the same string as from first    

      $('#'+iid).html('56789'); //not working! whyyyyyyyy? :)

   });    
</script>
  • 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-27T01:29:04+00:00Added an answer on May 27, 2026 at 1:29 am

    The first string is “actually” '#hello\[1\]\[2\]_world'.

    What happens is that you have to escape \s in the first case so that the $() function receives \[ and so on so that it knows that it should treat those characters as part of the id.

    In the second case you’re creating a string that’s hello\\\\[1\\\\]\\\\[2\\\\]_world (as you have 4 backslashes in the replacement!) that becomes, after the backslashes have been escaped, hello\\[1\\]\\[2\\]_world.

    You can confirm this by adding these lines at the end (look in the JS console):

    console.log( iid );
    console.log( 'hello\\[1\\]\\[2\\]_world' );
    

    http://jsfiddle.net/qSpaK/7/

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

Sidebar

Related Questions

We all know how the adjacent selector works (p+p), but I wonder if there's
I have a file selector (which works as needed) but I wanted to add
I need a CSS selector that can find the 2nd div of 2 that
Example: I have a selector like this, which I give to another method as
I have this selector: $(table.details tbody tr:not(.details)) But I'm wondering why this inner table
for example: <li class=list id=first-list></li> var li = document.getElementById(first-list); matchSelector(li, li.list); // this should
I can't find a selector suited for my needs, that is get the first
Lots of similar questions but non with a solution that works in my case.
I'm trying to use wordpress api, but have unknown problem with my request: First
first of all: my question is very theoretical. Even if I post an example,

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.