jquery escape square brackets to select element

2024/2/27 8:41:52

Consider a input element

<input id="meta[152][value]" type="text" />

Here the input field is dynamically generated. I need to select that field. So I used,

alert($('#meta[152][value]').val());

But this seems to be invalid. After searching I found, that the "square brackets" need to be escaped like #meta\\[152\\]\\[value\\]

So how to do that ? I currently use this code,

var id = "#meta[152][value]" // (I get this value by another method) I need the escaping to be done here. So that i can use as

/** I need the value of id to be escaped using regex,replace or any other methodto get #meta\[152\]\[value\] and not manually **/

alert($(id).val());

Your suggestions will be helpful !

Answer

The following should work:

alert($('#meta\[152\]\[value\]').val());

or

var id = "#meta\[152\]\[value\]";
alert($(id).val());

Working Example

Conversion Function:

function ConvertValue(id)
{var test = id.replace(/[[]/g,'\\\\[');return "#" + test.replace(/]/g,'\\\\]'); 
}

Conversion Example

http://en.ppmy.cn/q/42067.html

Related Q&A

Ember 1.0.0 input type radio checked binding not updated when property changes

Ive got a problem: Ive got this property called active on one of my models, it is either the string yes or no, now I want to use this property to check a HTML radio button. So when active is yes it sho…

Bind text to property of child object

Using knockout.js, is it possible to bind to a property of a child object of a JSON object from the server? Specifically, if Im given an object from the server that looks like this:var obj = {list: […

Include PHP file using jQuery or Javascript

I have a Javascript file that submits a form and loads an alert etc. Now I want to add functionality to call or get a .php file, I tried using $.get("mail.php"); but it doesnt seem to work.Ja…

Angular route with html5Mode giving Not found page after reload

I made some Angular routes as shown in the code bellow.app.config(function($routeProvider, $locationProvider, $provide) {$routeProvider.when(/, {templateUrl: home.html,controller: AppCtrl});.when(/port…

Vue Router, GitHub Pages, and Custom Domain Not Working With Routed Links

My domain: myname.com My GitHub repo: myname My GitHub name: myname Underlying GH-Pages URL: myname.github.io/myname My Issue: I have the following pages set up with a History Vue Router: home, con…

How to find and replace an object with in array of objects

I have an array of objects like this.[{"id": 1,"name": "January","abc": abc,"xyz": xyz }, {"id": 2,"name": "February",&qu…

why {key:value}[key] doesnt work?

1:{key:value}["key"] 2:({key:value})["key"] Im wondering how the JS interpreter works on the above codes, and why 1 doesnt work and why 2 works?

Chart Js clickable bar

I want to make my chart js bar clickable. I want to add click features such as links. No idea how to proceed. Have read documentation times 10...<script> var ctx = document.getElementById(myChart…

react select not recognizing default value

I have a react select component that isnt recognizing the default value option. The code looks like this:renderPlans(){if(this.props.plans){let list = this.props.plans.map(item=>{return ({label:it…

Absolute panel in R shiny gets hidden behind the leaflet output

I am trying to make a leaflet map full screen and also add filter controls on top of the map. However, when I try to do this my filter control(absolute panel) gets hidden behind the leaflet output duri…