'jquery string to number back to string
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
Solution 1:[1]
to convert int to string you can use in javaScript.
var string1 = ""+i;
& for converting string to int go for
var integ = parseInt(string1);
Solution 2:[2]
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);
});
Solution 3:[3]
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pulah Nandha |
| Solution 2 | Andy |
| Solution 3 | Giuseppe Garassino |
