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

6
  • Try putting the PHP output in as HTML and see if you get the same error.
    – Nick Zuber
    Dec 4, 2015 at 4: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 4, 2015 at 4:34
  • @Nick. Ah it works. something wrong with the php ?
    – Elbert
    Dec 4, 2015 at 5:44
  • Problem Solved @Elbert ? Dec 4, 2015 at 6:39
  • @Nana Not yet. The first text area does the alert when given an input. The other dont
    – Elbert
    Dec 4, 2015 at 6:50

2 Answers 2

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

  }
}
?>
2
  • It works. but after using getElementByTagName :). Thanks
    – Elbert
    Dec 4, 2015 at 7:50
  • Glad. It worked. Yeah TagName is also one option. Thanks @Elbert Dec 4, 2015 at 7:51
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);

    }
}
2
  • are u echoing or typing anything in between text-area or no..?? Dec 4, 2015 at 5:39
  • try using document.getElementById("evatext").text; Dec 4, 2015 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.