-1

I wanted to make an input value same to above input val using jquery.The above input type val is coming from a function. But don't know why the second input value is being set to undeefined and console gives no error.

HTML :

<input type="hidden" name="date" id="transactiona_date">

I Tried jquery 1.

console.log($("#myDate").val());
$("#transactiona_date").val($("#myDate").val());
console.log($("#transactiona_date").val());

2.

console.log($("#myDate").val());
$('#transactiona_date').val($("#myDate").val());
console.log($("#transactiona_date").val());

I even tried changing id to class in jquery as well as in html

console.log($("#myDate").val());
$(".transactiona_date").val($("#myDate").val());
console.log($(".transactiona_date").val());

No matter what i do i always get this in console.

06-02-2018
undefined

Any help why it is behaving like this ?

Also the myDate is getting value from

<input style="width:98%;margin-left:1%;margin:right:1%" type="text" name="invoice_date" id="myDate" value="<?php echo date('d-m-Y'); ?>">

Also i have no other error showing in console.And i want to share that the following id's or there is no other thing or code containing a relation to this code in same page , still lets consider a possibility of something interfering with the code, can you suggest a suitable way to diagnose my problem or how can i check what's been wrong in my code.

Adern Nerk
  • 332
  • 3
  • 13
  • 1
    Can you replicate your issue in a working snippet? – gurvinder372 Feb 06 '18 at 12:49
  • that's the issue, its working in snippet, but not working in my code , can you suggest a method so that i can diagnose whats wrong with code. – Adern Nerk Feb 06 '18 at 12:54
  • You are not providing a [Minimal, Complete, and Verifiable] question (https://stackoverflow.com/help/mcve) – Renzo Calla Feb 06 '18 at 13:03
  • 2
    I can easily replicate this: https://jsfiddle.net/m0fjbL9q/ All I did was change the `id` so the jQuery selector comes up blank. Setting the val throws no error, but getting it returns `undefined`. So I guess you need to double-check your selector actually matches the `` –  Feb 06 '18 at 13:05
  • @chris i found your comment useful, but i just copied the code from my notepad++ and pasted here, exactly without changing. So how come it matches input here but not there ? – Adern Nerk Feb 06 '18 at 13:09
  • @AdernNerk your code seems fine.Have you tried cleaning the cache?Maybe your browser loads an older version of your JS. – Alator Feb 06 '18 at 13:13
  • 1
    @AdernNerk The usual reason for that is that you're calling the jQuery code before the element exists. If that JS code is not wrapped in `onload` or similar and in a ` –  Feb 06 '18 at 13:14
  • wow i just putted it below cause script was between both elements, its fixed thanks, I didn't knew about that. – Adern Nerk Feb 06 '18 at 13:16
  • i didn't knew about the above hence i didn't knew about its already been answered because of the fact i was not having knowledge hence i thought its because of some kind of interference of another code , all i wanted was a way to diagnose.But never thought that mistake could be so silly – Adern Nerk Feb 06 '18 at 13:30
  • @AdernNerk It's not a big deal, don't worry about it. I will mention though that what put me on the right track was looking at the api doc for `.val()` and learning about when it returns `undefined`. I will *also* mention that your question did stump me quite a bit at first :) –  Feb 06 '18 at 13:42

1 Answers1

0

The usual reason for that is that you're calling the jQuery code before the element exists. If that JS code is not wrapped in onload or similar and in a that's further up than your , it won't find the element.

Solution: just write <script> below <input>

Adern Nerk
  • 332
  • 3
  • 13