2

I using javascript to extract the value from the SPAN element, then put it in a hidden form field, and then submit the data but why am I getting this result?

<form onsubmit="CDMFOCT()" id="CDMFOCTform">
    <div class="CDMFOCT"></div>
    <span class="CDMFOCT-span"></span>
    <input type="hidden" name="CDMFOCTtimer" id="CDMFOCTtimer" value="not yet defined"> 
</form>

Javascript:

function CDMFOCT() {
    CronSaati = $('.CDMFOCT-span').html();
    $("#CDMFOCTtimer").val(CDMFOCTtimer);
    $("#CDMFOCTform").submit();
};

Output:

Time: [object HTMLInputElement] will...
Editor
  • 622
  • 1
  • 11
  • 24

2 Answers2

1

The are two problem in your code

  1. $("#CDMFOCTtimer").val(CDMFOCTtimer); should be replaced with $("#CDMFOCTtimer").val(CronSaati); to give the hidden field value of your span.

  2. you have set CronSaati as a variable. var CronSaati = $('.CDMFOCT-span').html();

So Try this

$("#CDMFOCTform").submit(function() {
  var CronSaati = $('.CDMFOCT-span').html();
  $("#CDMFOCTtimer").val(CronSaati);

  // just for showing the html content of your span has been inserted into hidden input field
  alert($("#CDMFOCTtimer").val());

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="CDMFOCTform" method="post" action="">
  <div class="CDMFOCT"></div>
  <span class="CDMFOCT-span">Hello</span>
  <input type="hidden" name="CDMFOCTtimer" id="CDMFOCTtimer" value="not yet defined">
  <input type="submit" name="CDMFOCTsubmit">
</form>
Sidharth Gusain
  • 1,473
  • 1
  • 13
  • 20
0

Using JavaScript & jQuery to extract the value of span:

var node = $('.CDMFOCT-span')[0].childNodes[0].nodeValue;

Edit: Or just simply:

var node = $(.CDMFOCT-span).text();

Read more about how to get text node of an element in this link

and now putting it in the hidden form field:

$("input#CDMFOCTtimer").val(node);
Community
  • 1
  • 1
eylay
  • 1,712
  • 4
  • 30
  • 54
  • Working but all input was the same, I tried `input[type=hidden,id=CDMFOCTtimer]` it's possible? – Editor Sep 08 '16 at 11:24
  • @Saracoglu what do you mean? – eylay Sep 08 '16 at 11:54
  • I have multiple inputs, therefore all inputs set content of `CDMFOCT-span` - Is it possible to give a direct address to the Input? – Editor Sep 08 '16 at 11:58
  • so specify which input type, for example give an id like *myInput* to the input tag and select the specified input tag by jquery like `$(input#myInput).val(node);` – eylay Sep 08 '16 at 12:01