1

I am trying to append to some text to my html and using the submit() doesn't seem to be working. I am also getting a strange message from jquery that toLowerCase is not a function. All the other scripts are commented out to make sure there is no interference.

Jquery:

var template = function(action){
 return '<div class="event"><p>'+ action +'</p></div>'
};
var display = function(input){
 var temp = input;
 if(temp !== "") {
 $('html').appendTo('.gameboard');
 } else {
 prompt("You need to imput an appropriate answer");
 }
 $('#action').val("");
};
var main = function(){
 $('form').submit(function(){
 var action = $('#action').val();
 var input = action.toLowerCase();
 var html = template(action);
 display(action);
 interact(input);
 return false;
 });
};
$(document).ready(main);

html:

<!DOCTYPE html>
<html>
<head>
 <title>Lord of the Deahsticks</title>
 <link href="css/text-game.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="info">
 <h1 class="title">Lord of the Deathsticks</h1>
</div>
<div class="gameboard">
 <div class="event">
 <p>Hello this is the game!</p>
 </div>
 <div class="event">
 <p>You are awoken by a sound...You need to get up already, you're going to be late for you shift at the slave factory!</p>
 </div>
</div>
<form>
 <input type="text" id="#action" name="what to do">
 <input type="submit" name="button">
</form>
<script src="js/controllers/controller.js"></script>
<script src="js/model/characters.js"></script>
<script src="js/JQuery/jquery-3.1.0.js"></script>
<script src="js/JQuery/text-game-JQuery.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</body>
</html>
Arun Kumar Mohan
11.9k3 gold badges27 silver badges47 bronze badges
asked Aug 7, 2016 at 2:46
1
  • 1
    You should always post the exact error message you're receiving; that conveys important, relevant information. "doesn't seem to be working" and "strange message" are not as helpful. Commented Aug 7, 2016 at 2:52

3 Answers 3

1

I am assuming that you want to append content when submit button is clicked.

When a form is submitted, the default browser action is to reload the page. So, you have to override the default browser behaviour using preventDefault on the event object.

Also remove the pound symbol in the value of id attribute.

<input type="text" id="#action" name="what to do">

In your display method, you're appending the string 'html' and not the actual html content which is held in the variable temp. Modify your display method to

var display = function(input){
if(input !== "") {
 var html = template(input);
$(html).appendTo('.gameboard');
} else {
 alert("You need to input an appropriate answer");
}
$('#action').val("");

};

Note that we are calling the template method inside the display method only if the user entered something. And then we append the html content in the variable html to the class gameboard.

Your main method should look like

var main = function(){
 $('form').submit(function(event){
 event.preventDefault();
 var action = $('#action').val();
 var input = action.toLowerCase();
 display(action);
 interact(input);
 return false;
 });
};

Working codepen: http://codepen.io/anon/pen/wWQwvm

answered Aug 7, 2016 at 4:39
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your answer and explanations! Helped me understand what I'm trying to do so much more!
0

"Also, my test html pages is in a refresh loop and I cannot seem the find the problem." If you submit a form, it will refresh the page :

 $('form').submit();

So :

$(document).ready(main);
var main = function(){
 $('form').submit();
 ...
};

Will refresh the page once loaded, so will load it continuously. I'm not sure about your goal concerning the submit action.

answered Aug 7, 2016 at 2:53

3 Comments

when I was reading over my code I just realised that, changed it and the reloading is gone!
Also check the appendTo() part ;)
I thought the content precedes the method when using appendTo instead of append. Also when the text input is submitted or clicked I want i to appear as a div in the screen
0

Remove # from the id

 <input type="text" id="#action" name="what to do">

to

<input type="text" id="action" name="what to do">
answered Aug 7, 2016 at 4:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.