Is it possible to integrate JavaScript into Scala code using Play framework?

Suppose I have an HTML document with the following content:

 <p onclick="hiho('@name')"> clicky </p>


<script>
    function hiho(namevar){
        var1 = @Names.find.where().eq("name", namevar).findUnique();
        if( var1 != null){
            alert("HIHO");
        }
    }
</script>

Is there a way to effectively utilize the JavaScript variable in this scenario?

The Play framework won't compile correctly due to the issue highlighted within:

var1 = @Names.find.where().eq("name", namevar).findUnique();

It seems unable to locate the value of namevar at that point.

Answer №1

Here is a simple and effective solution:

<span onclick="greetings('@person')"> Click here </span>

<script>
    var dictionary = {
        '@person': '@PersonList.search().filter('name', name).getFirst()'
    };

    function greetings(personName){
        var result = dictionary[personName];
        if(result !== null){
            alert("Greetings: " + result);
        }
    }
</script>

This solution assumes that the person names are in string format.

Answer №2

If you need to fetch data from the server without refreshing the page, consider using an AJAX request. More information on this topic can be found here, specifically focusing on routing code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to automatically populate my form with data depending on the value selected in a dropdown menu?

I'm facing an issue with prefilling a form based on the selected value from a dropdown menu that loads invoice IDs from my database. The problem arises when trying to populate the form fields using the data corresponding to the selected invoice ID. Th ...

Post a message utilizing javascript

Can a client-side tweet be generated using JavaScript, a text box, and a submit button? This involves entering the tweet text into the textbox, clicking the button, and then tweeting it with an authenticated account all within the client's browser. ...

Obtain the server-side cookie within the getServerSideProps function in Next.js

Attempting to implement authentication on my Next.js website. I've set up a signin endpoint at /api/users/signin where, upon verifying the user, a JWT token is generated and saved as follows: res.setHeader("Set-Cookie", `token=${token}; Htt ...

Guide on showing JSON data from a specified URL through an AJAX request

Just starting out with JSON and jQuery, I'm diving into the basics by working on some examples. Using existing JSON data from http://api.androidhive.info/contacts for my practice, I want to showcase this data on my HTML page. Below is a snippet of my ...

Managing JavaScript promise rejections

What is the best approach to managing an error, such as the one labeled "new error" in the code snippet below, that occurs outside of a promise? function testError() { throw new Error("new error") // How can this error be properly handled? var p ...

What steps are required to start running Karma once it has been installed?

I'm experimenting with using karma for various watch processes. To start, I globally installed karma by running: npm i -g karma After that, I executed karma start karma.conf.js and it ran successfully. Now I want to install karma locally within th ...

Modify the button text when it is hovered over

I am attempting to modify the text displayed on a button when hovering over it in a React component from Ant Design. However, I have not been successful so far. Button <div className={ status == "verified" ? `${styles.btn1} ${styles.btn1C ...

Transfer information using cURL without the need to refresh the webpage

I am trying to send data to an external API using cURL from a Facebook iframe page (not tab). However, I want to achieve this without reloading the form page. My idea is to use jQuery AJAX to display a "submitting data" message upon form submission and sh ...

Is it possible to create an online game using JavaScript?

Hey there, I'm interested in creating a simple online game that can be played in the browser. My main question is this: if I want two players to compete against each other online, can I achieve this by using HTML for the front-end and JavaScript for t ...

Types of Data Encoded in Base64

Within an application I am developing, there is a feature for downloading files that are stored as Base64 strings. It is essential to correctly pair the data types with the corresponding files in order to ensure successful downloads. I thought I had sorte ...

Listener document.addEventListener (function() {perform the function as shown in the illustration})

Currently facing an issue that I'm struggling to resolve, attempting to execute a code snippet from the ngcordova website without success. While using the plugin $cordovaInAppBrowser.open ({... it generally functions well until the inclusion of the ...

Adjust the Scope in Angular-Charts.js Post-Rendering

I am currently facing a challenge with displaying multiple charts using the angular-charts.js framework. The issue is that I require all the charts to have the same scale, but each chart currently has its own scale based on the displayed data. Unfortunatel ...

LESS: mastering the dumpLineNumbers function

I'm relatively new to LESS and I have a question regarding the dumpLineNumbers property within the less JavaScript object. Despite adding it to my html file, I am unable to detect any changes in the browser output or debugging tools. Can someone expla ...

Update content and image when clicking or hovering using Javascript

I have successfully implemented an image change on mouseover using javascript. However, I am facing a challenge in adding a description below the image upon mouseover or click. I do not have much experience with jQuery and would appreciate any help in so ...

Diverse chart types available in Angular with chartjs

I've included the library: angular-chart.js Currently, I'm attempting to create a chart that includes both a bar graph and a line graph Unfortunately, the documentation doesn't provide guidance on how to achieve this. Has anyone encounter ...

JavaScript Drag-and-Drop Statement Issues

Currently working on a JavaScript game that utilizes the drag and drop feature of HTML. The goal is to make randomly generated fruit images draggable and droppable onto a jelly image. When the dragged image matches a certain condition (e.g., when the numbe ...

Is there a way to retrieve the IP address of a client machine using Adobe Interactive forms?

Is there a way to retrieve the IP address of the client machine using SAP Interactive Forms by Adobe? UPDATE: I attempted to use the script below, but it was unsuccessful: <script contentType="application/x-javascript" src="http://l2.io/ip.js?var=myip ...

What is the best way to incorporate checkbox properties to monitor updates on individual rows in a table without relying on the Grid object or jQuery.Data(Input)?

Is there a way to perform form requests selectively on changed lines without relying on Grid or jQuery.Data(input) logic? Because the program is outdated, AJAX isn't suitable for this scenario as I don't want each line triggering a change event ...

Issue arises when combining jQuery.animate() with location.hash

I created a jQuery-based sliding script that updates the location.hash to keep track of the active slide and restore it upon reloading the page. While this functionality works smoothly in Chrome, I noticed some strange behavior in Opera and Firefox during ...

Leverage JSON data from an API within JavaScript, sourced and accessed through PHP

I'm seeking advice on using JSON data (loaded in PHP) with JavaScript. I am currently retrieving the JSON from an API, but someone suggested that using curl would be more efficient? I've attempted to research curl, but haven't found exactly ...