Elbert
Elbert

Reputation: 506

Textarea value is NULL javascript

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

Upvotes: 1

Views: 4231

Answers (2)

Nana Partykar
Nana Partykar

Reputation: 10548

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);

  }
}
?>

Upvotes: 2

Amin Kodaganur
Amin Kodaganur

Reputation: 666

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);

    }
}

Upvotes: 1

Related Questions