0

I am trying to render some text using node/express. I have a html file contains a form.

search.ejs

$(document).ready(function(){
    var userInput;
    $("#submit-button-id").click(function(){
        userInput = $1_11_1("#userInput").val();
        $.get("http://localhost:3000/searching", {
            userInput: userInput
        }, function(){});
    });
});

app.js

app.get('/searching', function(req, res){
    var userInput = req.query.userInput;

    /** I am able to get the userInput (the code above works) */

    res.send(userInput);

    /** I want to render the userInput (for instance, on localhost:3000/results) */
});

Any help/links would be greatly appreciated, thanks!

3 Answers 3

1

In your app.js, you are sending the userInput information back to search.ejs. this is the location:

$(document).ready(function(){
    var userInput;
    $("#submit-button-id").click(function(){
        userInput = $1_11_1("#userInput").val();
        $.get("http://localhost:3000/searching", {
            userInput: userInput
        }, function(){***User data can be used here***});
    });
});

So the following code would work to render your raw data into a div on your html page called render-div:

$(document).ready(function(){
    var userInput;
    $("#submit-button-id").click(function(){
        userInput = $1_11_1("#userInput").val();
        $.get("http://localhost:3000/searching", {
            userInput: userInput
        }, function(userInputData){
            $('.render-div).append(userInputData);

        });
    });
});`
Sign up to request clarification or add additional context in comments.

Comments

0

According to the documentation you have the response as a parameter in your callback, so you should try something like this:

$.get("http://localhost:3000/searching", {userInput: userInput},
    function(response){
      alert(response);
 });

Comments

0

If you're using EJS you can do it like this:

First, in your results.ejs file

<div id="displayResult"><h2><%= result %></h2></div>

Then, in your app.js

app.get('/searching', function(req, res){
var userInput = req.query.userInput;
res.render('results.ejs',{result:userInput});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.