2

Say I have the code:

$("#auto").append('<div id="' + results_id + '"></div>');

but I want auto to be a string:

var string="auto";

Then I want to use it as:

$("\'+string+'\").append('<div id="' + results_id + '"></div>');

I don't know why, but this is throwing some error.

Someone hinted me that we must be using escape string so as to use it in this way, but then don't know why, I am without any luck.

2
  • 6
    $("#"+string).append("..."); Commented Mar 6, 2013 at 10:10
  • why not $("#"+string).append('<div id="' + results_id + '"></div>'); Commented Mar 6, 2013 at 10:11

10 Answers 10

1

do it like this:

$('#' + string)
Sign up to request clarification or add additional context in comments.

Comments

1

This is what you are looking for:

var string = "auto";
$('#' + string).append('<div id="' + results_id + '"></div>');

Comments

1

jQuery allows you to use variables for selectors without a problem. You just need to remember to call a correct element, so if you call for ID remember to use # before your string variable value.

var string="auto";
$("#"+string).append('<div id="' + results_id + '"></div>');

Comments

0

Write:

$('#' + string).append('<div id="' + results_id + '"></div>');

Comments

0

You have qoutes not correctly nested and also you miss the sharp sign. Maybe try something like this:

$("#"+string).append('<div id="' + results_id + '"></div>');

Comments

0
<script>
 var string = 'auto';
 $("#"+string).append('THE STRING YOU WANT');
</script>

<div id="auto"><div>

I hope this helps.

Comments

0

You don't actually have to use jQuery to resolve the symbol itself. And you can create your <div /> in a nicer way:

$(document.getElementById(string))
    .append($('<div />', {id: results_id}));

Comments

0

The basic issue is that you are confusing variable names and their values.

"#auto" is a string.

If I declare a variable like:

var myString = "auto";

then notice that the variable myString contains the value, "auto".

In jQuery, you can get an element (a div in our example below) to manipulate by calling it with $("#auto") - meaning I want the element of the page that has been declared thus on the HTML page:

<div id="auto">...</div>

But since you can pass a string as an argument to the special $("#") function so that whatever follows the # gets grabbed from the page.

Therefore, instead of fixed and pre-determined arguments like $("#auto"), you can use $("#"+myString) from the example above to achieve the same result, but now by manipulating the value of myString you can do lots of things with the same $("#"+myString) without bothering about the exact element names over and again.

Comments

0
var string = "auto";
$('#' + string).append(.....);

Hope this is the solution for your question.

And can check the below link to know more on selectors.

https://css-tricks.com/forums/discussion/9766/jquery-selector-div-variable/p1

Comments

0

Write:

var myString = '#' + MYID;
$(myString).append('<div id="' + results_id + '"></div>');

"some explanation"

Hope this helps!

1 Comment

Please include some explanation in your answers rather than just code. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.