Pass Java variable to JavascriptExecutor argument for jQuery function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Pass Java variable to JavascriptExecutor argument for jQuery function



Hi I am using selenium webdriver where I try to execute jQuery script by passing java variable to access valid id.



here is the code.


int move = 3;
String date = "2011-03-05";
String script = "$('#date_of_birth-'+move+).datepicker('update', '+date+')";
js.executeAsyncScript(script, 1000);



I want to use both move and date variable inside jQuery function.





Hi, what is the source of your confusion? It seems like you are unable to concatenate strings in Java. If that's the case, have a look at this Q&A: stackoverflow.com/questions/3753869/…
– Erwin Bolwidt
Aug 13 at 3:28




3 Answers
3



After long struggles and debugging I found the solution.



here is the right way to pass java variable to jQuery function.


String script = "$('#date_of_birth-" + move + "').datepicker('update', '" + date + "')";





You should delete this, I think, since you just copied my answer and made a small change.
– Tim Biegeleisen
Aug 13 at 4:09





this small change make the script works.
– OMore
Aug 13 at 4:15



If you want the string concatenation to take place within Java itself, then it has to happen outside of the intended JavaScript string. What you are currently doing is to intercalate the literal strings move and date inside your JavaScript string.


move


date


String script = "$('#date_of_birth-'" + move + "').datepicker('update', '" + date + "')";



But, we can use a StringBuilder here which would make things easier to read (and possibly run faster):


StringBuilder


StringBuilder sb = new StringBuilder("");
sb.append("$('#date_of_birth-'");
sb.append(move);
sb.append("').datepicker('update', '");
sb.append(date);
sb.append("')");
String script = sb.toString();





still not working :( the script is working without passing java variables String script = "$('#date_of_birth-1').datepicker('update', '2018-08-15')"; but when I pass variables nothing update on datepicker.
– OMore
Aug 13 at 3:55



String script = "$('#date_of_birth-1').datepicker('update', '2018-08-15')";





you edit your answer but it still not working test on your system.
– OMore
Aug 13 at 14:39



Can you try below snippet in your application which may works i guess:-



Exsisting problem:
$('#date_of_birth-'3).datepicker('update', '2011-03-05');



Current solution:
$('#date_of_birth-3').datepicker('update', '2011-03-05');



snippet:
int move = 3;
String date = "2011-03-05";


String scripting = "$('#date_of_birth-" + move + "').datepicker('update', '" + date + "')";

System.out.println(scripting);

StringBuilder sb1 = new StringBuilder("");
sb1.append("$('#date_of_birth-");
sb1.append(move+"'");

sb1.append(").datepicker('update', '");
sb1.append(date);
sb1.append("')");

String script1 = sb1.toString();
System.out.println(script1);





Duplication of my answer.
– Tim Biegeleisen
Aug 13 at 5:25






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard