Strobe_
Strobe_

Reputation: 515

Trying to escape quotes in a href tag

I have the following line of code,

$("#busdata").append("<div><a href="url">" + data.response.results[i].webUrl + "</a></div>");

I essentially want the "data.response.results[i].webUrl" to replace the url string, but I'm not quite sure how to escape the quotes properly.

Upvotes: 5

Views: 8503

Answers (9)

guest271314
guest271314

Reputation: 1

Escaping quotes is not necessary

$("#busdata")
.append("<div><a href=" 
  + data.response.results[i].webUrl 
  + ">" 
  + data.response.results[i].webUrl 
  + "</a></div>"
);

Upvotes: 2

Quentin
Quentin

Reputation: 943216

This is most easily achieved by not building HTML by smashing strings together in the first place.

$("#busdata").append(
    $("<div />").append(
        $("<a />").attr("href", data.response.results[i].webUrl)
    )
);

Upvotes: 2

hkasera
hkasera

Reputation: 2148

You could do this :

 $("#busdata").append("<div><a href='"+data.response.results[i].webUrl +"'>" + data.response.results[i].webUrl + "</a></div>");

Since you are using double quotes for the string to append, you can use single quotes around the variable in the href attribute and then add that variable.

Upvotes: 2

Hitmands
Hitmands

Reputation: 14169

Simply do it following my example:

var a = $('<a />', {
href: 'url here',
text: 'text here'
}); $('body').append(a);

Upvotes: 2

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You can store it in variable instead :

var url = data.response.results[i].webUrl;
$("#busdata").append("<div><a href='"+url+'">" + url + "</a></div>");

Hope this helps.

Upvotes: 3

gurvinder372
gurvinder372

Reputation: 68393

a single quote ' and a string concatenator +

$("#busdata").append("<div><a href='"+ data.response.results[i].webUrl +"'>" + data.response.results[i].webUrl + "</a></div>");

Upvotes: 3

AlexD
AlexD

Reputation: 4290

You can escape quotes by replacing them with \" or just use single quotes - '

So "<div><a href="url">" becomes "<div><a href=\"url\">" or "<div><a href='url'>"

Upvotes: 4

Shubham
Shubham

Reputation: 1793

if url is a variable $("#busdata").append("<div><a href='" + url +"'>" + data.response.results[i].webUrl + "</a></div>");

and if you want to write by yourself

$("#busdata").append("<div><a href='url'>" + data.response.results[i].webUrl + "</a></div>");

Upvotes: 2

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Your syntax is wrong. You need to escape quotes. Change your <a href="url"> to <a href=\"url\"> like this:

$("#busdata").append("<div><a href=\"url\">" + data.response.results[i].webUrl + "</a></div>");

Or if you feel that's a bit tough, you can exchange the quotes, ' for ":

$("#busdata").append('<div><a href="url">' + data.response.results[i].webUrl + "</a></div>");

Else, if you are trying to add the URL from the response:

$("#busdata").append("<div><a href=\"" + data.response.results[i].webUrl + "\">" + data.response.results[i].webUrl + "</a></div>");

Upvotes: 2

Related Questions