Pass a string parameter in an onclick function
It looks like you're building DOM elements from strings. You just need to add some quotes around result.name:
'<input type="button" onClick="gotoNode(\'' + result.name + '\')" />'
You should really be doing this with proper DOM methods though.
var inputElement = document.createElement('input');
inputElement.type = "button"
inputElement.addEventListener('click', function(){
gotoNode(result.name);
});
document.body.appendChild(inputElement);
Just be aware that if this is a loop or something, result
will change before the event fires and you'd need to create an additional scope bubble to shadow the changing variable.
How to pass parameters to OnClickListener?
Use your own custom OnClickListener
public class MyLovelyOnClickListener implements OnClickListener
{
int myLovelyVariable;
public MyLovelyOnClickListener(int myLovelyVariable) {
this.myLovelyVariable = myLovelyVariable;
}
@Override
public void onClick(View v)
{
//read your lovely variable
}
};
Android: How to pass parameters from OnClickListener to another
The most simple way is to declare global variable. Declare your ac
outside of onCreate scope instead of inside of it.
public Double ac; // global variable
@Override
public void onCreate(Bundle savedInstanceState){
Button Calculate = (Button) theLayout.findViewById(R.id.button);
Button buttonb = (Button) theLayout.findViewById(R.id.buttonb);
final TextView tvac = (TextView) theLayout.findViewById(R.id.tvac);
final TextView tvh = (TextView) theLayout.findViewById(R.id.tvh);
final EditText eta = (EditText) theLayout.findViewById(R.id.eta);
final EditText etn = (EditText) theLayout.findViewById(R.id.etn);
final EditText etb = (EditText) theLayout.findViewById(R.id.etb);
Calculate.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Double a = new Double(eta.getText().toString());
Double n = new Double(etn.getText().toString());
ac = a*n;
tvac.setText(getResources().getString(R.string.tvresultados2) + " " + ac);
}
});
buttonb.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
double b = new Double(etb.getText().toString());
double h = ac; //assign global variable into h
tvh.setVisibility(View.VISIBLE);
tvh.setText("h = " + h);
}
});
}
Passing parameter from method to OnClickListener
Change
public void Proses(String x)
to
public void Proses(final String x)
Passing parameter from a button to android:onClick method
public void printNo( View v ) {
switch (v.getId()) {
case (R.id.Button_1):
//stuff
break;
case (R.id.Button_2):
//stuff
break;
case (R.id.Button_3):
//stuff
break;
}
How do I pass the onClick parameter in this one?
I'm not familiar with compose but assuming it works like any other kotlin you could write it like this
OTPScreen(navController = navController, onClick = {mobileNum, otp ->
//your code here
})
as noted by Karsten Gabriel, this can also be written shorter as
OTPScreen(navController) { mobileNum, otp ->
//your code here
}
Or, if you want to use an existing function like
fun myFunction(mobileNum: String, otp: String) {
//your code here
}
for example you can use that like
OTPScreen(navController = navController, onClick = ::myFunction)
How to pass the button value into my onclick event function?
You can pass the value to the function using this.value
, where this
points to the button
<input type="button" value="mybutton1" onclick="dosomething(this.value)">
And then access that value in the function
function dosomething(val){
console.log(val);
}
Android - How to pass arguments to DialogInterface onClickListener?
If you make the parameter final, you can access it within anonymous classes
public void onSwiped(@NonNull final RecyclerView.ViewHolder viewHolder, int direction) {
Otherwise, you must sub-class DialogInterface.OnClickListener
to add a constructor that can accept a ViewHolder as a field
Related Topics
Display a Part of the Webpage on the Webview Android
How to Use Mkfifo Using Android's Ndk
Cli on Dalvikvm Fails on Jni Lib
Android Draw Route on a Mapview with Twoo Poi-S
Android Material Chip Component Crashing App. Unable to Inflate Xml
Java.Lang.Noclassdeffounderror: Failed Resolution Of: Landroid/Support/V4/Os/Buildcompat
Android - Navigation View Item Menu Background Color
Swap Fragment in an Activity via Animation
Multiple Table SQLite Db Adapter(S) in Android
How to Disable Crashlytics During Development
How to Get a Fragment to Remove Itself, I.E. Its Equivalent of Finish()
Nested Fragments Disappear During Transition Animation
Android Mediaplayer Problems :"Error (-38 , 0) " and "Stop Called in State 1"