473,503 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can a function swap form field contents?

Hi there,
I was wondering if anyone had come across some javascript that would
allow me to have a chart of say 20 music tracks, and be able to move
each track up and down the chart using up/down links? I am sure I saw
one about 3 years ago.

Consider this:
A table with a form around it. Each row of the table is numbered 1 to
20. There will be a form field on each row for Artist, Title, Label, Mix
and Genre. There will also be an up and down arrow to move the contents
of each row up and down.

I need to create 2 javascript functions. 1 to swap the contents upwards,
and one to swap downwards.

The link to go up or down will pass the row number to the function, and
the fields that need swapping. The function will then take the values
and swap them over.

Is this possible? And will it work cross-browser cross-platform? Any ideas?

Thank you!

Steve
Sep 14 '05 #1
3 2278
Lee
Dooza said:

Hi there,
I was wondering if anyone had come across some javascript that would
allow me to have a chart of say 20 music tracks, and be able to move
each track up and down the chart using up/down links?


I've never come across one, but it's pretty simple, using very
standard cross-browser scripting:

<html>
<head>
<title>Demo</title>
<script type="text/javascript">
function move(f,dir,pos) {
var artist=f.artist[pos].value;
var title=f.title[pos].value;
f.artist[pos].value=f.artist[pos+dir].value;
f.title[pos].value=f.title[pos+dir].value;
f.artist[pos+dir].value=artist;
f.title[pos+dir].value=title;
}
</script>
<body>
<form>
<table>
<tr><th>#</th><th>Artist</th><th>Title</th><th>&nbsp;</th></tr>
<tr>
<td>1</td>
<td><input name="artist" value="Alpha"></td>
<td><input name="title" value="Apple"></td>
<td><input type="button" value="down" onclick="move(this.form,1,0)"></td>
</tr>
<tr>
<td>2</td>
<td><input name="artist" value="Beta"></td>
<td><input name="title" value="Banana"></td>
<td><input type="button" value="down" onclick="move(this.form,1,1)">
<input type="button" value="up" onclick="move(this.form,-1,1)"></td>
</tr>
<tr>
<td>3</td>
<td><input name="artist" value="Gamma"></td>
<td><input name="title" value="Grape"></td>
<td><input type="button" value="up" onclick="move(this.form,-1,2)"></td>
</tr>
</table>
</form>

Sep 14 '05 #2
Dooza wrote:
Hi there,
I was wondering if anyone had come across some javascript that would
allow me to have a chart of say 20 music tracks, and be able to move
each track up and down the chart using up/down links? I am sure I saw
one about 3 years ago.

Consider this:
A table with a form around it. Each row of the table is numbered 1 to
20. There will be a form field on each row for Artist, Title, Label, Mix
and Genre. There will also be an up and down arrow to move the contents
of each row up and down.

I need to create 2 javascript functions. 1 to swap the contents upwards,
and one to swap downwards.

The link to go up or down will pass the row number to the function, and
the fields that need swapping. The function will then take the values
and swap them over.

Is this possible? And will it work cross-browser cross-platform? Any ideas?


Here's one that just swaps rows of a table:

<script type="text/javascript">

function moveR( el, x )
{
while ( el.parentNode && 'tr' != el.nodeName.toLowerCase() ){
el = el.parentNode;
}
var t = el.parentNode;
var i = el.rowIndex + x;
if ( i < 0 ) i += t.rows.length;
if ( i == t.rows.length ) i = 0;
t.replaceChild(t.removeChild(el), t.insertRow(i));
}

</script>

<table>
<tr>
<td>
<input type="button" value="Move up" onclick="moveR(this, -1);">
<input type="button" value="Move down" onclick="moveR(this, 1);">
</td>
<td>row 0</td>
</tr>
<tr>
<td>
<input type="button" value="Move up" onclick="moveR(this, -1);">
<input type="button" value="Move down" onclick="moveR(this, 1);">
</td>
<td>row 1</td>
</tr>
<tr>
<td>
<input type="button" value="Move up" onclick="moveR(this, -1);">
<input type="button" value="Move down" onclick="moveR(this, 1);">
</td>
<td>row 2</td>
</tr>
</table>

--
Rob
Sep 14 '05 #3
Hi Lee,
Thats almost spot on. There are some hidden form fields that are need
too, but I can see where I can add them into the function. Thank you
very much for your help!

Steve

Lee wrote:
Dooza said:
Hi there,
I was wondering if anyone had come across some javascript that would
allow me to have a chart of say 20 music tracks, and be able to move
each track up and down the chart using up/down links?

I've never come across one, but it's pretty simple, using very
standard cross-browser scripting:

<html>
<head>
<title>Demo</title>
<script type="text/javascript">
function move(f,dir,pos) {
var artist=f.artist[pos].value;
var title=f.title[pos].value;
f.artist[pos].value=f.artist[pos+dir].value;
f.title[pos].value=f.title[pos+dir].value;
f.artist[pos+dir].value=artist;
f.title[pos+dir].value=title;
}
</script>
<body>
<form>
<table>
<tr><th>#</th><th>Artist</th><th>Title</th><th>&nbsp;</th></tr>
<tr>
<td>1</td>
<td><input name="artist" value="Alpha"></td>
<td><input name="title" value="Apple"></td>
<td><input type="button" value="down" onclick="move(this.form,1,0)"></td>
</tr>
<tr>
<td>2</td>
<td><input name="artist" value="Beta"></td>
<td><input name="title" value="Banana"></td>
<td><input type="button" value="down" onclick="move(this.form,1,1)">
<input type="button" value="up" onclick="move(this.form,-1,1)"></td>
</tr>
<tr>
<td>3</td>
<td><input name="artist" value="Gamma"></td>
<td><input name="title" value="Grape"></td>
<td><input type="button" value="up" onclick="move(this.form,-1,2)"></td>
</tr>
</table>
</form>

Sep 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1161
by: vagle | last post by:
(Probably old issue but I didn't find a posting) Simple case: 1. I have a PDF file I made with Acrobat PRO. It contains a form with two fields, "Name" and "TestScore". 2. From my PHP file on...
2
1958
by: Gary | last post by:
Morning all, I have a form field called: Bsk01 How do I onBlur prompt the user to enter a ZERO as character one, if one is not already entered. At the same time, I would like to ensure at...
4
6287
by: Jonathan Strange | last post by:
I have a website that uses Forms, and if I complete the form fields, submit the form, realise that there was an error or omission, and then hit the Back button, the form fields are all empty. This...
3
28996
by: MX1 | last post by:
I have a query written in MS Access that has a few calculated fields. Is it possible to refer to that query in a form field. I'd like the form field to show the sum of one of the columns from the...
14
3461
by: Paul | last post by:
I want to set the page title and/or a form hidden field programatically through ASP.Net. I do not want to use something like... <% sTitle ="My Title" %> <html><title><%=sTitle%></title>..... ...
5
13270
by: Stuart | last post by:
Hi all, Iv'e got a page that has a mass amount of input fields, all of which require a decimal figure. To make it easier when it comes to inputting data, I'm trying to setup + and - links that...
3
4320
by: idle | last post by:
#include<stdio.h> void swap(int &, int &); void main(){ int num1, num2; printf("Enter 2 numbers > "); scanf("%d%d", &num1, &num2);
1
2009
by: jayfriend | last post by:
Hi All, I would like to swap the contents of two large files without using any temporary file. However, a buffer can be used instead of temporary file. I could swap the file contents, but have a...
1
1322
user404
by: user404 | last post by:
I'm trying to add a button that will update the value of a form field with some text followed by the contents of another field. Here's what I got: <form action="<?php echo ($_SERVER); ?>"...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.