2

So I've been searching a lot here and on google, but can't seem to find the solution.

Onclick of a link, I need to empty the value of a hidden input field with a certain ID.

var x = document.getElementById('tijdelijke_datum');

document.getElementById("close-popup").onclick = verwijderdatum;

function verwijderdatum() {
  x.value = "";
}
<a href="websitelink" id="close-popup">Button value</a>

<input type="hidden" id="tijdelijke_datum" value="{$profile.x}" />
connexo
  • 53,704
  • 14
  • 91
  • 128
Sybrentjuh
  • 247
  • 3
  • 14
  • Does this answer your question? [Update hidden input field value with another input field value?](https://stackoverflow.com/questions/14709637/update-hidden-input-field-value-with-another-input-field-value) – Kiksen Jan 28 '20 at 13:05
  • Why does `close-popup` have `href="websitelink"`? Also, did your forget to add relevant tags? This looks like you're using some kind of template library: `value="{$profile.x}"` – connexo Jan 28 '20 at 13:08
  • @Kiksen unfortunately not, but thanks! – Sybrentjuh Jan 28 '20 at 14:35
  • @connexo I just typed that there instead of the link I was using :-) The value has a smarty value assigned to it – Sybrentjuh Jan 28 '20 at 14:35

4 Answers4

4

Its working. Your problem is, since you are using anchor tag, the page navigates to new URL. Try using Button instead or setting href to "#"

var x = document.getElementById('tijdelijke_datum');

document.getElementById("close-popup").onclick = verwijderdatum;
document.getElementById("close-popup2").addEventListener("click", verwijderdatum);

function verwijderdatum() {
  console.log(document.getElementById('tijdelijke_datum').value);
  x.value = "";

  console.log(document.getElementById('tijdelijke_datum').value);
}
<a href="#" id="close-popup">Button value</a>

<br>

<input type="button" id="close-popup2" value="Button value">

<input type="hidden" id="tijdelijke_datum" value="{$profile.x}" />
Aditya Bhave
  • 998
  • 1
  • 5
  • 10
1

Change your javascript code like below,

var x = document.getElementById('tijdelijke_datum');

document.getElementById("close-popup").addEventListener('click', () => {
    event.preventDefault(); // To stop href to navigate, we are using this line
    verwijderdatum();
});

function verwijderdatum() {
    x.value = "";
}
Gangadhar Gandi
  • 2,162
  • 12
  • 19
0

Change <a href="websitelink" id="close-popup">Button value</a> to <a href="javascript:verwijderdatum();" id="close-popup">Button value</a>

Remove this line: document.getElementById("close-popup").onclick = verwijderdatum;

Vijay Joshi
  • 919
  • 7
  • 17
-1

Use Jquery to reset the input field.

use the following code :

<script>

  $("#close-popup").click(function(){
    $("#tijdelijke_datum").val("")
  })
</script>

Also include a jquery CDN file in your project.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

Your final code will be as follow :

<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<div class="container text-center">
<a href="websitelink" id="close-popup">Button value</a>
<input type="hidden" id="tijdelijke_datum" value="{$profile.x}" />
</div>


<script>
  $("#close-popup").click(function(){
    $("#tijdelijke_datum").val("")
  })
</script>
Shashikant
  • 103
  • 3
  • I never mind also getting a jquery solution, atleastI can learn from that :-) Although in this case I indeed only need the js, to keep it clean. Thanks though – Sybrentjuh Jan 28 '20 at 15:39