-2

How to select a div in jquery using id. That id which is store in variable.

<div id="d3">
  some thing is here...
</div>

jquery Code:

var id = 3;
$("#d" + id)  

I am using this code but this is not working.

3 Answers3

3

This i working.

Example: Fiddle

HTML:

<div id="d3">
  some thing is here...
</div>

Javascript:

var id = 3;
$("#d" + id).html("Change it!"); 
Johan Hoeksma
  • 3,534
  • 5
  • 28
  • 40
2

Your code is correct and getting the element by the selector check it here, you need to check following.

Live Demo

var id = 3;
alert($("#d" + id).html()); // shows some thing is here... in alert.
Community
  • 1
  • 1
Adil
  • 146,340
  • 25
  • 209
  • 204
  • No this code is not working. jquery is added and I am using it in dom ready.Please check yourself – user3672518 May 30 '14 at 10:59
  • 2
    @user3672518 Who should check it is you, not someone who's trying to help you. Check the comments in your question. There's a `jsFiddle` link with the actual code working. – emerson.marini May 30 '14 at 11:00
  • When you say "Your code is correct", the existing code does nothing you can see (it just obtains a JQuery element then discards it) :) – iCollect.it Ltd May 30 '14 at 11:04
  • The statement "this is not working" does not gives any clear hint that what is happening? you might be getting some error that can help in finding the solution – Adil May 30 '14 at 11:06
  • I check the console and it give error "Uncaught Error: Syntax error, unrecognized expression: #d4" " – user3672518 May 31 '14 at 11:10
0

If you need to select a div here is the code,If i understand your correctly your want to select the text some thing is here...?

<div id="d3">
  some thing is here...
</div>

$(function() {
    var id = 3;
    $("#d3" + id);
});
billah77
  • 196
  • 1
  • 1
  • 13