How to escape square brackets in jQuery selector?


To escape square brackets in jQuery selectors is quite easy. Let’s see with an example of escaping the brackets in the name of the <select> box.

Example

Whatever you will select, will get added to the textarea on button click.

Live Demo

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {

    $("#addnow").click(function () {
       $("#myselect :selected").each(function () {
          $("#text").val($("#text").val() + $(this).text() + "
");        });     }); }); </script> </head> <body> <select name="selectemail[]" multiple="true" id="myselect">     <option value="email">email@example.com</option>     <option value="name">David</option> </select> <textarea id="text"></textarea> <button id="addnow">Select and Click to Add</button> </body> </html>

Amit D
Amit D

e

Updated on: 18-Dec-2019

291 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements