简体   繁体   中英

how to define value from within the function call

I have this function call:

<a href="javascript;;" onclick="helloWorld(value="planet")">

and then this function

function helloWorld(value) {
  return value
}

Obv, it doesn't work. I need to be able to set the value of value from within the onclick . Is this possible in js?

The value that you are passing is known as arguments and the placeholder that is receiving the value is known as parameter .

So whatever you pass arguments gets assigned to a placeholder (ie to parameter) in this case it is value

So you have defined a function who accept a parameter, so all you have to do is call it with single argument.

 function helloWorld(value) { return value }
 <a href="javascript;;" onclick="helloWorld('planet')"> link </a>

When you call your function with return to value it means result of helloWorld('planet') will be your value , in this case planet . When you pass argument to functions you just need to put in correct order, you do not need to define them with = .

Let's say you have function with 3 values like this

function test(value1, value2, value3) {
   return value1+value2+value3
}

you need to just respect to order test(5,2,1) to identify each value

 function helloWorld(value) { console.log(value) //Here as you can see you already have acces to it return value //Also here when you call this function it return to value it means, result of helloWorld('anyString') will be your value }
 <a onclick=helloWorld("planet")> TEST </a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM