473,383 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Copy table cell text into an input as value attribute

I'm trying to write a simple JavaScript function which will use a
button to copy table cell data into a form input text box as the
"value" attribute.

The following is intended to test the function, but doesn't work. I'm
new to this - previous JavaScript experience mostly copy & paste -
could somebody please point to where I'm going wrong.

~~~~~~~~~~~~~~~~~~~~~~~~~
<head>
<title>Testing JavaScript</title>
<script language="JavaScript">
function copytext(source_id, dest_id)
{
var s = getElementById(source_id);
var d = getElementById(dest_id);
var text = s.innerText;

d.setAttribute("value", text);
}
</script>
</head>
<body>
<h1>Testing JavaScript</h1>
<form>
<table>
<tr>
<td id="cell1">
<b>a string of text</b>
</td>
<td>
<button onclick="copytext('cell1', 'input1')">>></button>
</td>
<td>
<input type="text" id="input1" width="20">
</td>
</tr>
</table>
</form>
</body>
</html>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Many thanks for any helpful suggestions

CB

Jun 22 '06 #1
2 13805
ch**************@ntlworld.com wrote:
I'm trying to write a simple JavaScript function which will use a
button to copy table cell data into a form input text box as the
"value" attribute.

The following is intended to test the function, but doesn't work. I'm
new to this - previous JavaScript experience mostly copy & paste -
could somebody please point to where I'm going wrong.

~~~~~~~~~~~~~~~~~~~~~~~~~
<head>
<title>Testing JavaScript</title>
<script language="JavaScript">
The language attribute is deprecated, type is required:

<script type="text/javascript">

function copytext(source_id, dest_id)
{
var s = getElementById(source_id);
Please don't indent using tabs when posting, use 2 (preferred) or 4 spaces.

getElementById is a method of the DOM 2 Core document interface:

var s = document.getElementById(source_id);

It is also best to test features that may not be supported by some browsers:

if (document.getElementById){
var s = document.getElementById(source_id);
...
var d = getElementById(dest_id);
var d = document.getElementById(dest_id);

var text = s.innerText;
The innerText property is IE proprietary and not widely implemented.
The W3C equivalent is textContent, which naturally isn't implemented by
IE. Alternative utilities have been posted here, search the archives
for 'innerText textContent'.

d.setAttribute("value", text);
setAttribute is known to be a bit buggy on some browsers - though I
don't think there should be a problem here. Anyhow, it's much simpler
to write:

d.value = text;

}
</script>
</head>
<body>
<h1>Testing JavaScript</h1>
<form>
The action attribute is required for HTML forms, even if it has no value:

<form action="">
[...] <button onclick="copytext('cell1', 'input1')">>></button>


Inside a form, a button element is by default a submit button. So even
if the value is copied to the input, the form is posted to the current
page, causing it to re-load and clear the value.

<URL:http://www.w3.org/TR/html4/interact/forms.html#edef-BUTTON>
If you don't want it to be a submit button, give it a type attribute
value of 'button'. Anyhow, it seems better to use an input with
type="button" and avoid the issue:

<input type="button" value=">>"
onclick="copytext('cell1', 'input1')">

[...]

--
Rob
Jun 22 '06 #2
Not sure if you ever found your answer, but this will work in IE and Firefox:

<html>
<head>
<title>Testing JavaScript</title>
<script language="JavaScript">
function copytext(source_id, dest_id) {
var s = document.getElementById(source_id);
var d = document.getElementById(dest_id);
d.value=s.innerHTML;
}
</script>
</head>
<body>
<h1>Testing JavaScript</h1>
<form>
<table>
<tr>
<td id="cell1">a string of text</td>
<td><input type="button" value=">>" onclick="copytext('cell1', 'input1')" /></td>
<td><input type="text" id="input1" width="20"></td>
</tr>
</table>
</form>
</body>
</html>
Jul 13 '06 #3

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

Similar topics

5
by: JeffFinnan | last post by:
Is there a way to have a text input field equal some variable value so that it changes if that variable changes? It does not have to by dynamic. <INPUT TYPE="TEXT" NAME="hW" size="4" value=some...
2
by: Pete Kipe | last post by:
I'm not a JavaScript programmer...but I'm trying to put together a simple menu system for a new website and need a little help. I have the following script: <SCRIPT language=javascript> <!--...
3
by: harry | last post by:
I want to be able to change the text alignment within a table cell between "right" & "center" depending on how many rows are in the table. Is this possible in Javascript? - can't see how to do...
0
by: John Alexander | last post by:
Hi, I have a text file I need to import into Postgres, v. 7.2. The file has a header row. I want to begin the import at the line below the header row. Is this possible, or will I have to...
0
by: Mark A. Sam | last post by:
Hello, Whenever I add a table, the text position of the cells is centered. I would like the default to be left-top. Is there a way to set this? Thank you and God Bless, Mark A. Sam
2
by: charles-brewster | last post by:
I have progressed the copytext function test page a little since my original post here in the small hours this morning. It now opens a debugging window so I can try to figure out what it is or...
3
by: Todor Vachkov | last post by:
Dear all, here is the code: ----------------------------- <tr>         <td>ohne Anmeldung: </td>         <td><input name="vanm" id="vanm" type="checkbox"...
1
by: asafok | last post by:
hi, i'm tring to get access to a cell inside a table that has the vlaue of an Email address but is not clickable. i tring to go to that cell and Wrap the inner text with <a href="mailto... tag so...
4
by: ukfusion | last post by:
Hi, Its something stupidly simple but for some reason i cant find a straight forward cross browser solution. I've got 2 sets of address fields and i want a tick box that when the user clicks it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.