Still trying to copy table cell text into an input as value attribute

Discussion in 'Javascript' started by charles-brewster@ntlworld.com, Jun 22, 2006.

  1. Guest

    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 isn't doing.

    Current version of the test page:
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    <head>
    <title>Testing JavaScript</title>

    <script language="JavaScript">
    var _console = null;
    function debug(msg)
    {
    if ((_console == null) || (_console.closed))
    {
    _console =
    window.open("","console","width=600,height=300,resizable,screenX=50,screenY=50");
    _console.document.open();
    }
    _console.focus();
    _console.document.writeln(msg);
    }
    </script>

    <script language="JavaScript">
    function copytext(source_id, dest_id)
    {
    debug('source_id is ' + source_id + '<br>');
    debug('dest_id is ' + dest_id + '<br>');

    var s = document.getElementById(source_id);
    var d = document.getElementById(dest_id);
    var the_text = s.innerHTML;
    debug('the_text is [' + the_text + ']<br>');

    d.value = the_text;
    // d.value = "some string data"
    }
    </script>

    </head>
    <body>
    <h1>Testing JavaScript</h1>
    <form>
    <table border="1" cellspacing="1" cellpadding="1">
    <tr>
    <td id="cell1">
    a string of text
    </td>
    <td>
    <button onclick="copytext('cell1', 'input1');">>></button>
    </td>
    <td>
    <input type="text" id="input1" width="20">
    </td>
    </tr>

    <tr>
    <td colspan="3" align="center">
    <input type="button" value="test debug" onclick="debug('Testing
    debug(msg)<br>');">
    </td>
    </tr>

    </table>
    </form>
    </body>
    </html>
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    When passing a string literal to the <input>'s value attribute
    (commented out in the script above) I do see the text flash in the
    input text box on the screen momentarily, then disappear. When passing
    the JavaScript variable containing the text I actually want to go in
    there, nothing (at least in Netscape 7).

    I tried replacing the function as it is above with
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    <script language="JavaScript">
    function copytext(source, dest)
    {
    var new_input = document.createElement("input");
    var parent = dest.parentNode;

    var the_text = source.innerHTML;
    debug('the_text is [' + the_text + ']<br>');
    var the_type = dest.getAttribute("type");
    debug('the_type is [' + the_type + ']<br>');
    var the_id = dest.getAttribute("id");
    debug('the_id is [' + the_id + ']<br>');
    var the_width = dest.getAttribute("width");
    debug('the_width is [' + the_width + ']<br>');

    new_input.setAttribute("type", the_type);
    new_input.setAttribute("id", the_id);
    new_input.setAttribute("width", the_width);
    new_input.setAttribute("value", the_text);
    debug('new_input is [' + new_input + ']<br>');

    parent.replaceChild(new_input, dest);
    }
    </script>
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    and calling with

    <button
    onclick="copytext(document.getElementById('cell1'),
    document.getElementById('input1'));">
    >>

    </button>

    The debug window again shows the values being picked up OK in the
    function, but no change happening at all on the page.

    Any helpful suggestions would be most welcome

    Many thnaks
    CB



    Any helpful suggestions or pointers would be much appreciates
    , Jun 22, 2006
    #1
    1. Advertising

  2. RobG Guest

    wrote:
    > 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 isn't doing.


    [...]

    See my reply to your first post.

    Regarding a function for getting the textContent/innerText of an
    element, try here:

    <URL:http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thread/9c8d5b2796cc437f/6366e98e3a71cdf7?q=innerText+textContent+robg&rnum=1#6366e98e3a71cdf7>


    --
    Rob
    RobG, Jun 22, 2006
    #2
    1. Advertising

  3. Guest

    RobG wrote:
    > See my reply to your first post.
    >


    Seen - and very many thanks to you

    > Regarding a function for getting the textContent/innerText of an
    > element, try here:
    >
    > <URL:http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thread/9c8d5b2796cc437f/6366e98e3a71cdf7?q=innerText+textContent+robg&rnum=1#6366e98e3a71cdf7>


    That worked fine, and changing to <input type="button" ....> causes the
    value to stay where it's put after clicking.

    For your interest the page I'm hoping to use this on is here:
    http://www.iearneurope.org/lingo/index.php
    It should save users a bit of typing.

    If you'd like a mention on the 'acknowledgements' page on the site I'm
    happy to oblige.

    Next JavaScript job on there will be a translation popup window with
    (hopefully) click-and-drag to allow a user to rearrange word-order in a
    type="text" <input> before sending a different language rendition of
    some english text into a database.

    I daresay you'll see me back here once I get started on that!

    With many thanks for your help

    CB
    , Jun 22, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phillip Roncoroni
    Replies:
    14
    Views:
    1,091
    Toby A Inkster
    Apr 5, 2004
  2. Replies:
    1
    Views:
    4,275
    Joe Kesselman
    Aug 2, 2006
  3. Replies:
    8
    Views:
    522
  4. sdf
    Replies:
    3
    Views:
    1,904
  5. Replies:
    1
    Views:
    131
Loading...

Share This Page