[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how can i make this in ajax

Posted on 2009-02-15
9
Medium Priority
?
195 Views
Last Modified: 2012-05-06
lets say i have a script and eatch time i call the script it gives me a number
so the first time i call it using an ajax it gives me the num "1"
the second time i get the number "2" and soo on
---------------------------------------------------------------------------------
and i want the number to show like this on the windows
<table><tr><td>2</td></tr><tr><td>1</td></tr></table
-----------------------------------------------------------------------------------
and if  i call the script agean and for example i get  the num "10"
i want the table to add the num 10 in the top of the table and the table will look like this now

<table><tr><td>10</td></tr><tr><td>2</td></tr><tr><td>1</td></tr></table

and so on
0
Comment
Question by:mgtm3
  • 6
  • 3
9 Comments
 
LVL 5

Expert Comment

by:striker46
ID: 23645830
How does the script give you the number? is it an auto-increment (indicating the # of times you called the script)? or it retreieves it from elsewhere?
0
 

Author Comment

by:mgtm3
ID: 23645876
dont worry about the script  - lets say it is random
0
 
LVL 5

Expert Comment

by:striker46
ID: 23645902
As the code you need for this seems to be JavaScript, this script might help you get what you're looking for. It is an example of how to add rows to an existing table.

http://javascript.internet.com/miscellaneous/add-a-row.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mgtm3
ID: 23645939
thanks but in this example the rows goes in the bottom i want the new rows to go in the top
0
 
LVL 5

Expert Comment

by:striker46
ID: 23645973
Check here:

http://www.javascriptkit.com/javatutors/dom2.shtml

Especially the bottom, where it says:



// If we had wished to add it to the      beginning of the DIV, we could have used

insertBefore(txt,document.getElementById('myDiv').firstChild);
0
 
LVL 5

Expert Comment

by:striker46
ID: 23646036
Call the function as insRow(0) to insert the new row at the very top. insRow(X), X is the position to insert the new row.

Function code below


Source: http://michaelsync.net/2006/07/04/adding-new-row-to-the-existing-table-javascript




function insRow(rowIndex)
{
if(rowIndex == null)rowIndex = 0;
var x=document.getElementById("t1").insertRow(rowIndex)
var y=x.insertCell(0)
var z=x.insertCell(1)
y.innerHTML="NEW CELL1"
z.innerHTML="NEW CELL2"
}

Open in new window

0
 
LVL 5

Expert Comment

by:striker46
ID: 23646046
A full example I made using the code above, for you to test how it works. Just copy and paste to a HTML file and run.



<html>
 
<head>
<script type="text/javascript">
 
function insRow(rowIndex)
{
if(rowIndex == null)rowIndex = 0;
var x=document.getElementById("myTable").insertRow(rowIndex)
var y=x.insertCell(0)
var z=x.insertCell(1)
y.innerHTML="NEW CELL1"
z.innerHTML="NEW CELL2"
}
 
</script>
 
</HEAD>
 
<BODY>
 
<a href="javascript:addRow('myTable')">Add row</a>
 
<table id="myTable" cellspacing="0" border="1">
  <tbody>
    <tr>
      <td>row1_column1</td><td>row1_column1</td>
    </tr>
  </tbody>
</table>
 
 
 
<p><input type="button" value="Botón" onClick="insRow(0)" name="B3"></p>
 
 
 
 
 
</body>
 
</html>

Open in new window

0
 
LVL 5

Accepted Solution

by:
striker46 earned 2000 total points
ID: 23646053
Line 22 played no role in the code above and is misleading. Here it is.

<html>
 
<head>
<script type="text/javascript">
 
function insRow(rowIndex)
{
if(rowIndex == null)rowIndex = 0;
var x=document.getElementById("myTable").insertRow(rowIndex)
var y=x.insertCell(0)
var z=x.insertCell(1)
y.innerHTML="NEW CELL1"
z.innerHTML="NEW CELL2"
}
 
</script>
 
</HEAD>
 
<BODY>
 
<table id="myTable" cellspacing="0" border="1">
  <tbody>
    <tr>
      <td>row1_column1</td><td>row1_column1</td>
    </tr>
  </tbody>
</table>
 
<p><input type="button" value="Add row" onClick="insRow(0)" name="B3"></p>
</body>
 
</html>

Open in new window

0
 

Author Closing Comment

by:mgtm3
ID: 31547128
thanks man
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question