0

I have anchor tag in my html file and I am calling function on click of anchor tag

function myFunction(text) {
  event.preventDefault();
  document.getElementById("demo").innerHTML = "text";
  window.open("www.amazon.com", '_blank')
}
<a href="javascript:void(0)" onclick="myFunction('test')">Click me</a>
<p id="demo"></p>

This is the example of what I am trying to do in my scenario. As I have already passed parameter to the function so cannot pass event as parameter in function. I am using implicit "event" of java script function. Its working fine but I have never used implicit event.

I need to know whats the difference between implicit and explicit event in JavaScript function? I need to know in depth meaning of implicit event which is present in context of the function rather than the one which is passed to the function

Bhaktuu
  • 89
  • 1
  • 7

1 Answers1

-1

If you want to keep the onchange in the HTML code see snippet below. The main changes are:

  • You need 2 parameters:

    • event for .preventDefault()
    • text form the text value
  • Use the parameter as a variable, not a string literal: document.getElementById("demo").innerHTML = text;

function myFunction(event, text) {
    event.preventDefault();
    document.getElementById("demo").innerHTML = text;
    //window.open("www.amazon.com", '_blank')
}
<a href="javascript:void(0)" onclick="myFunction(event, 'test')">Click me</a>

<p id="demo"></p>
raul.vila
  • 1,984
  • 1
  • 11
  • 24