1

Hi I am using PHP 5 as a back end language with Jquery in front end. In one of the database, we have auto generated numbers as a primary key. Now we are calling that number as a id to the div in HTML. But it looks like div doesn't accept number.

Now i want to transfer that number into some string, which i can pass through the div. Again we have to convert that string into same earlier number so that we can query the database.

the html code looks as below

<div id = "something">

<span class = "class1">...</span>
...
...
...
<span class = "class5"> ...</span>


</div>

please note that

$( "#something").children(".class5").html("response"); 

doesn't work here since something is integer...hope i am clear

here "something" is a integer which i wish to convert into string. After getting inside div, i want to convert string into the same earlier number.

Now guide me how to convert int to string so that i can put in div ..and then again that string back to same int....

i have two options...to use jquery/javascript ..or through php....thank you

user1590595
  • 695
  • 2
  • 12
  • 33
  • 1
    your question is not clear at all, but if you are trying to give a div an id which is a number, you can't. Instead of `id=0`, try `id=s0` (i.e. prepend the number with some letter or word). Then it will work. See here: http://stackoverflow.com/a/79022/1180785 – Dave Sep 21 '13 at 11:10
  • 4
    @Dave that is just not true. Modern browsers (even IE6) should work fine with numeric "id" values, and it's explicitly allowed in HTML5. That answer you reference is 5 years old. – Pointy Sep 21 '13 at 11:11
  • @Pointy is right. It is a common misunderstanding. Only blanks / whitespace is not allowed according to the specs (at least last time I checked up on this) – davidkonrad Sep 21 '13 at 11:13
  • `After getting inside div`: what does this mean? – Andy Sep 21 '13 at 11:13
  • What's your problem? `(".class" + number)` ? – djot Sep 21 '13 at 11:15

3 Answers3

0

to convert int to string you can use in javaScript.

var string1 = ""+i;

& for converting string to int go for

var integ = parseInt(string1);
Pulah Nandha
  • 768
  • 4
  • 23
0

its pretty simple, do it like this - suppose

(something) id = 5 

in case.

setup the id for the div in php as

$divid = "div".$id

and

$divclass = $id

then pass these to div.

it will become

<div id="div5" class="5" >

do whatever using

$("#div5")

in jquery and when you want the id back use

var id = $("#div5").attr('class');

to convert the numbers to string and vice versa use:

var string = String(1);

and

var num = Number(string);
Giuseppe Garassino
  • 2,214
  • 1
  • 23
  • 42
PNK
  • 3
  • 3
0

If I'm reading you right, you want access to the id of the div. Here, something is an integer. It gets converted to a string when you use it - like I have in the example below - to a string for use as the id. Then you can get the id of that div element again inside of the class html using some jQuery and affixing a + to the beginning of it to convert from a string back to an integer.

var something = 1;

$("#" + something).children(".class5").html(function() {
  var id = +$(this).closest('div').attr('id');
  $(this).html('Typeof id: ' + typeof id + id);
});

Fiddle

Andy
  • 39,764
  • 8
  • 53
  • 80