Reputation: 791
I have a GET request that fetches some information from a server-side function and appends the data to a series of divs. However, when I console.log the response, I'm given the entire HTML for the website, as opposed to just the values I need.
Request
$('#verify').click(function() {
var parameters = {
firstName: $('#firstName').val(),
lastName: $('#lastName').val(),
email: $('#email').val() };
$.get('/verify', parameters, function(data) {
console.log(data); //Returns full body of HTML
$('#firstLast').val(data); // Doesn't work
});
});
Console.log
<!DOCTYPE html>
<html>
<head></head>
<title>
Demo
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="Demo project">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<style type="text/css"></style>
</title>
<body>
<form method="post" action="post" id="plans" class="plans"><input id="firstName" type="text" name="firstName" value="rhys"><input id="lastName" type="text" name="lastName" value="edwards"><input id="email" type="text" name="email" value="@rhysedwards.com"><input id="submit" type="submit" name="submit" value="submit"></form>
<div id="firstLast">[email protected]: false</div>
<div id="firstDotLast">[email protected]: false</div>
<div id="fInitialLastName">[email protected]: false</div>
<div id="fInitialDotLastName">[email protected]: false</div>
<div id="firstNameLInitial">[email protected]: false</div>
<div id="firstNameDotLInitial">[email protected]: false</div>
<div id="firstNameOnly">[email protected]: true</div>
<div id="lastNameOnly">[email protected]: false</div>
<div id="firstNameUnderscoreLastName">[email protected]: false</div>
<div id="fInitialUnderscoreLastName">[email protected]: false</div>
<div id="firstNameUnderscoreLInitial">[email protected]: false</div>
<input id="verify" name="verify" value="verify" type="submit"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script><script src="./javascripts/script.js"></script>
</body>
</html>
Inside that console.log is what I need:
<div id="firstLast">[email protected]: false</div>
<div id="firstDotLast">[email protected]: false</div>
<div id="fInitialLastName">[email protected]: false</div>
<div id="fInitialDotLastName">[email protected]: false</div>
<div id="firstNameLInitial">[email protected]: false</div>
<div id="firstNameDotLInitial">[email protected]: false</div>
<div id="firstNameOnly">[email protected]: true</div>
<div id="lastNameOnly">[email protected]: false</div>
<div id="firstNameUnderscoreLastName">[email protected]: false</div>
<div id="fInitialUnderscoreLastName">[email protected]: false</div>
<div id="firstNameUnderscoreLInitial">[email protected]: false</div>
Jade template:
block content
form(method='post', action='post', class='plans', id='plans')
input#firstName(type='text', name='firstName' value="rhys")
input#lastName(type='text', name='lastName' value="edwards")
input#email(type='text', name='email' value="@rhysedwards.com")
input#submit(type='submit', name='submit', value='submit')
#firstLast !{resultA}
#firstDotLast !{resultB}
#fInitialLastName !{resultC}
#fInitialDotLastName !{resultD}
#firstNameLInitial !{resultE}
#firstNameDotLInitial !{resultF}
#firstNameOnly !{resultG}
#lastNameOnly !{resultH}
#firstNameUnderscoreLastName !{resultI}
#fInitialUnderscoreLastName !{resultJ}
#firstNameUnderscoreLInitial !{resultK}
input#verify(name='verify', value='verify', type='submit')
How do I get the div value for each of these from the data
parameter?
Upvotes: 0
Views: 636
Reputation: 113
Since you are putting data to a div
element use $('#firstLast').html(data);
.val() //is used for input elements
Upvotes: 2