1

i want to append a tag to with different ids, for eg.

<div id="1"></div>
<div id="2"></div>
<div id="3"></div>
<div id="4"></div>

if i use $('div').append('<span>Hello World</span>); then it attached the <span> tag with every <div>, but i want to attach it to only 1 div based on it id. for example some time to 4 some times to 3 etc.

Jelo Melo
  • 52
  • 2
  • 10

2 Answers2

5

Use the wonderful id selector offered by JQuery:)

$("#1").append('<span>Hello World</span>');
Samson
  • 2,801
  • 7
  • 37
  • 55
3

Just use the ID selector:

$('#1').append('<span>Hello World</span>');

Edit: IF you don't use HTML5:

You can't use numbers in IDs or classes when they don't follow a char. So you need use id="div1" instead:

$('#div1').append('<span>Hello World</span>');
WolvDev
  • 3,182
  • 1
  • 17
  • 32
  • HTML5 specification supports numbers as IDs. – VisioN Jul 14 '12 at 19:38
  • Numbers only? I read the post from Kishore in the other comment and there it says the same: ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters – WolvDev Jul 14 '12 at 19:41
  • The working draft for HTML 5 is even more permissive, saying only that an id must contain at least one character and may not contain any space characters. (character == digit or letter?) – WolvDev Jul 14 '12 at 19:41
  • @ShogunArts.de Yep, you can use even special characters. – VisioN Jul 14 '12 at 19:45
  • Was something new to me with the numbers only :) – WolvDev Jul 14 '12 at 19:45
  • Edited my answer in case someone is reading it and don't use html5 – WolvDev Jul 14 '12 at 19:48