-2

var base = "/some/url?search=";
var value = '"VALUE IN QUOTES"';
$('#inner').append('<a href=' + '/some/url?search=' + value + '>link</a>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="inner"></div>

Hi.

My task is to add a double quoted parameters to request url. The resulting url should be

some/url?search="QUOTED VALUE WITH SPACES" "ANOTHER VALUE"

In other words it should be a single string with double quotes.

Thanks for any help.

kpeek
  • 29
  • 5

1 Answers1

1

You have two problems.

  1. Encoding data to put in URLs
  2. Generating a DOM including that.

To encode the data, use encodeURIComponent

var base = "/some/url?search=";
var value = '"VALUE IN QUOTES"';
var url = base + encodeURIComponent(value)

Then to build the DOM, use DOM methods (or jQuery wrappers around them). Don't mash strings together.

$('#inner').append(
    $("<a />").attr("href", url).text("link")
);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335