1

I have a foreach to get messages in database,My code is here:

<form action="/Send/Message/Reply" method="POST" id="sendReply">
    {{csrf_field()}}
  <textarea class="text-right" cols="80" rows="5" id="messageReply">
  </textarea>
  <input type="hidden" value="{{$message->id}}" id="messageId">
  <div class="footer text-right">
       <button type="submit" class="S-products">Send</button>
   </div>
</form>

Now I Want to get message id and Reply_Content in javascript with axios by this code:

(function()
 {
document.querySelector('#sendReply').addEventListener('submit',function (e) {
   var messageReply = document.querySelector('#messageReply').value;
   var messageId = document.querySelector('#messageId').value;
   console.log(messageId)
   axios.post(this.action,{
        'messageReply' : messageReply,
        'messageId' : messageId,
         '_token': $('input[name=_token]').val()
          })
   })
   })();

Bu i get messageId as Undefined,How I can Fix this problem?

Hanie Asemi
  • 790
  • 1
  • 5
  • 15
  • 1
    Can you please just show the log of document.querySelector('#messageId') – Ullas Hunka Aug 08 '18 at 06:58
  • Is this angular project? – Plochie Aug 08 '18 at 07:01
  • What is the exact error? Access property of undefined? variable is undefined? Note your code is inside an IIFE, so if this code is above where your elements are, ie ` – Patrick Evans Aug 08 '18 at 07:03
  • @UllasHunka log of document.querySelector('#messageId') is undefined – Hanie Asemi Aug 08 '18 at 07:17
  • @PatrickEvans Thanks this worked for me – Hanie Asemi Aug 08 '18 at 07:20

1 Answers1

0

I fixed my problem with this code:

 var messageId = document.getElementById('messageId').value;
Hanie Asemi
  • 790
  • 1
  • 5
  • 15
  • `querySelector('#messageId')` and `getElementById('messageId')` are effectively the same thing they both find the first element with an id value of `messageId`. You would of had to have changed some other code, changed its position in the html document, etc – Patrick Evans Aug 08 '18 at 15:08