Problem passing string to function

Discussion in 'Javascript' started by M.L., May 6, 2010.

  1. M.L.

    M.L. Guest

    Basically I'm trying to convert a paragraph within a table cell into a
    textarea within that same cell, keeping the paragraph text intact.

    However, when I pass the text string to a function called
    makeTextarea, both Firefox and IE8 give me the error: 'test_text' is
    undefined. I need help determining what I'm doing wrong. Thanks.

    function calcRows()
    {
    var tbody =
    document.getElementById("myTable").getElementsByTagName("tbody")[0];
    var numRows = tbody.rows.length;
    for (var counter = 0; counter < numRows; counter++)
    {
    if
    (tbody.rows[counter].cells[1].getElementsByTagName("textarea")[0])
    {
    var ptext = "test_text";
    tbody.rows[counter].cells[1].innerHTML = '<p
    onClick="makeTextarea('+counter+','+ptext+')">'+ptext+'</p>';
    }
    }
    }

    function makeTextarea(rownum,mytext)
    {
    var tbody =
    document.getElementById("myTable").getElementsByTagName("tbody")[0];
    tbody.rows[rownum].cells[1].innerHTML = '<textarea
    name="item_name_"'+rownum+'" class="border3b" rows="5"
    maxlength="500">'+mytext+'</textarea>';
    }
     
    M.L., May 6, 2010
    #1
    1. Advertising

  2. M.L.

    David Mark Guest

    M.L. wrote:
    > Basically I'm trying to convert a paragraph within a table cell into a
    > textarea within that same cell, keeping the paragraph text intact.
    >
    > However, when I pass the text string to a function called
    > makeTextarea, both Firefox and IE8 give me the error: 'test_text' is
    > undefined. I need help determining what I'm doing wrong. Thanks.
    >
    > function calcRows()
    > {
    > var tbody =
    > document.getElementById("myTable").getElementsByTagName("tbody")[0];
    > var numRows = tbody.rows.length;
    > for (var counter = 0; counter < numRows; counter++)
    > {
    > if
    > (tbody.rows[counter].cells[1].getElementsByTagName("textarea")[0])
    > {
    > var ptext = "test_text";
    > tbody.rows[counter].cells[1].innerHTML = '<p
    > onClick="makeTextarea('+counter+','+ptext+')">'+ptext+'</p>';

    ^

    Right there. What did you forget? You could have saved some time by
    logging the string.
     
    David Mark, May 6, 2010
    #2
    1. Advertising

  3. M.L.

    M.L. Guest


    >> Basically I'm trying to convert a paragraph within a table cell into a
    >> textarea within that same cell, keeping the paragraph text intact.
    >>
    >> However, when I pass the text string to a function called
    >> makeTextarea, both Firefox and IE8 give me the error: 'test_text' is
    >> undefined. I need help determining what I'm doing wrong. Thanks.
    >>
    >> function calcRows()
    >> {
    >> var tbody =
    >> document.getElementById("myTable").getElementsByTagName("tbody")[0];
    >> var numRows = tbody.rows.length;
    >> for (var counter = 0; counter < numRows; counter++)
    >> {
    >> if
    >> (tbody.rows[counter].cells[1].getElementsByTagName("textarea")[0])
    >> {
    >> var ptext = "test_text";
    >> tbody.rows[counter].cells[1].innerHTML = '<p
    >> onClick="makeTextarea('+counter+','+ptext+')">'+ptext+'</p>';

    > ^
    >
    >Right there. What did you forget? You could have saved some time by
    >logging the string.


    Forget what? Sorry but my JS is rusty. And what does "logging the
    string" mean?
     
    M.L., May 7, 2010
    #3
  4. M.L.

    M.L. Guest


    >>>> However, when I pass the text string to a function called
    >>>> makeTextarea, both Firefox and IE8 give me the error: 'test_text' is
    >>>> undefined. I need help determining what I'm doing wrong. Thanks.
    >>>>
    >>>> function calcRows()
    >>>> {
    >>>> var tbody =
    >>>> document.getElementById("myTable").getElementsByTagName("tbody")[0];
    >>>> var numRows = tbody.rows.length;
    >>>> for (var counter = 0; counter < numRows; counter++)
    >>>> {
    >>>> if
    >>>> (tbody.rows[counter].cells[1].getElementsByTagName("textarea")[0])
    >>>> {
    >>>> var ptext = "test_text";
    >>>> tbody.rows[counter].cells[1].innerHTML = '<p
    >>>> onClick="makeTextarea('+counter+','+ptext+')">'+ptext+'</p>';
    >>> ^
    >>>
    >>>Right there. What did you forget? You could have saved some time by
    >>>logging the string.

    >>
    >> Forget what? Sorry but my JS is rusty. And what does "logging the
    >> string" mean?

    >
    >Term: logging
    >Interpretation: saving it in a place where you can analyze it when
    >things go wrong. For example, in Firefox+Firebug you could use
    >console.log(). In many other cases, a simple alert() will do.


    I was already using the Firefox Error Console, which showed me that
    the input string was undefined, so I thought "logging the string"
    referred to something different.

    >Term: the string
    >Interpretation: the value you assign to the cell's innerHTML property.
    >
    >Term: forgetting
    >Interpretation: there's something missing in the string; more
    >specifically, in the value of the onclick attribute. Look at this:
    > 'onClick="makeTextarea('+counter+','+ptext+')"'
    >After the concatenation, this could look like:
    > onClick="makeTextarea(0,test_text)"
    >Now when you click the paragraph, makeTextarea will _not_ be called,
    >because test_text does not exist.
    >
    >Recommendation: quote the ptext argument.
    >
    >
    ></exegesis> :)


    Thanks for the additional clues, but I'm just not getting something
    right, even after quoting the argument.

    '<p onClick="makeTextarea('+counter+',\"'+ptext2+'\")">'

    Firefox Error Console: syntax error
    makeTextarea(0,
     
    M.L., May 8, 2010
    #4
  5. M.L.

    M.L. Guest


    >> Thanks for the additional clues, but I'm just not getting something
    >> right, even after quoting the argument.
    >>
    >> '<p onClick="makeTextarea('+counter+',\"'+ptext2+'\")">'
    >>
    >> Firefox Error Console: syntax error
    >> makeTextarea(0,

    >
    >The onclick attribute is delimited by double quotes, so you can't use
    >those inside the attribute value. It will come out as
    >
    > onClick="makeTextarea(0, "test_text")"
    >
    >The second " ends the attribute value. Try this:
    >
    > '<p onClick="makeTextarea(' + counter + ",'" + ptext2 + '\')">'


    Thanks for your patience Stefan. To help me better understand the
    quoting mix I replaced each escaped quote with its HTML entity
    character &quot;. It worked!

    '<p onClick="makeTextarea('+counter+',&quot;'+ptext+'&quot;)">'
     
    M.L., May 8, 2010
    #5
    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. Newsgroup - Ann
    Replies:
    5
    Views:
    668
    John Carson
    Jul 30, 2003
  2. James Vanns
    Replies:
    7
    Views:
    7,173
    Evan Carew
    Jan 21, 2004
  3. Foxy Kav
    Replies:
    1
    Views:
    478
    John Carson
    Apr 25, 2004
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,340
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
  5. Shashank

    Problem passing a string to a function

    Shashank, Dec 27, 2007, in forum: C Programming
    Replies:
    3
    Views:
    467
    Shashank
    Dec 27, 2007
Loading...

Share This Page