1

I want to display the text area value. I generate it with PHP code This code is in the looping

 for($i=0;$i<$piecount;$i++)
    {
    echo "<tr><td class=\"forma\" align=\"center\">".$compc[$i]."</td>";
    if($i==0)
    {
       echo "<td align=\"center\" class=\"forma\" rowspan=".$pcg."  style=\"\"> 
      <textarea name=\"eva\" id=\"evatext\"   class=\"textEva\" >
      </textarea> 
      <input type=\"image\"src=\"image/save.jpg\"class=\"imgClass\" onclick=\"EvaCek();\" name=\"save\"></td></tr>";

} }

Also I have a html code for dummy my id for inputdate which is displayed none

<input name="evadate" id="inputdate" style="display:none">

And I pass it to javascript

function EvaCek()
{
    var dateobj = new Date();
        var month = dateobj.getUTCMonth()+1;
        var day = dateobj.getUTCDate();
        var year = dateobj.getUTCFullYear();
        var tes=document.getElementById("inputdate").innerHTML= year+"-"+month+"-"+day;
        var bla=document.getElementById("evatext").value;

    if(bla!="")
    {
     alert(tes);
     alert(bla);

    }

}

When I type something into the text area and save it, it does the alert, but only the date, the text area is NULL. What is wrong in here? Thank you

Elbert
  • 474
  • 3
  • 13
  • Try putting the `PHP` output in as `HTML` and see if you get the same error. – Nick Zuber Dec 04 '15 at 04:29
  • check your "name" attributes match your "id" attributes so that getElementById are aligned. Also, use your browser's console to read errors rather than using alert, it'll save you tons of time. to do so open "developer tools" in your browser to see the console, then use console.log(tes) instead of alert(tes) – bob Dec 04 '15 at 04:34
  • @Nick. Ah it works. something wrong with the php ? – Elbert Dec 04 '15 at 05:44
  • Problem Solved @Elbert ? – Nana Partykar Dec 04 '15 at 06:39
  • @Nana Not yet. The first text area does the alert when given an input. The other dont – Elbert Dec 04 '15 at 06:50
  • Because you are accessing with ID. **ID Must Be Unique**. Check my answer. @Elbert – Nana Partykar Dec 04 '15 at 07:01

2 Answers2

2

It is because ID should be unique for each input.

IDs must be unique

For more info, check here Unique ID

So, access it through class name. I'm not seeing anywhere 'inputdate'. So, i didn't edited it. But, Don't access inputdate or textarea through ID. Because, forloop is creating numerous number of <textarea></textarea> and every textarea is having the same id. Avoid accessing with ID.

<?
for($i=0;$i<$piecount;$i++)
{
    echo "<tr><td class=\"forma\" align=\"center\">".$compc[$i]."</td>";
    if($i==0)
    {
        echo "<td align=\"center\" class=\"forma\" rowspan=".$pcg."  style=\"\"> 
            <textarea name=\"eva\" id=\"evatext\" class=\"textEva\" ></textarea> 
            <input type=\"image\"src=\"image/save.jpg\"class=\"imgClass\" onclick=\"EvaCek();\" name=\"save\">
        </td>
        </tr>";
    }
}
?>

<?
function EvaCek()
{
  var dateobj = new Date();
      var month = dateobj.getUTCMonth()+1;
      var day = dateobj.getUTCDate();
      var year = dateobj.getUTCFullYear();
            //change for inputdate. Access it through class name.
      var tes=document.getElementByClass("inputdate").innerHTML= year+"-"+month+"-"+day;
      var bla=document.getElementByClass("textEva").value;

  if(bla!="")
  {
   alert(tes);
   alert(bla);

  }
}
?>
Community
  • 1
  • 1
Nana Partykar
  • 10,175
  • 8
  • 43
  • 73
1

use this to get value of text area ...demo

document.getElementById("evatext").innerHTML; 

full code

function EvaCek()
{
    var dateobj = new Date();
    var month = dateobj.getUTCMonth()+1;
    var day = dateobj.getUTCDate();
    var year = dateobj.getUTCFullYear();
    var tes=document.getElementById("inputdate").innerHTML= year+"-"+month+"-"+day;
    var bla=document.getElementById("evatext").innerHTML;// use this

    if(bla!="")
    {
     alert(tes);
     alert(bla);

    }
}
Amin Kodaganur
  • 616
  • 5
  • 18