How to escape square brackets in jQuery selector?

jQueryWeb DevelopmentFront End Technology

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.


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

Live Demo

<!DOCTYPE html>
<script src=""></script>
$(document).ready(function() {

    $("#addnow").click(function () {
       $("#myselect :selected").each(function () {
          $("#text").val($("#text").val() + $(this).text() + "\n");

<select name="selectemail[]" multiple="true" id="myselect">
    <option value="email"></option>
    <option value="name">David</option>
<textarea id="text"></textarea>
<button id="addnow">Select and Click to Add</button>

Published on 16-Dec-2017 07:26:39