iori
iori

Reputation: 3506

How to print a value in console.log base on ID of an element?

<input id="email" name="email" type="text" value="[email protected]">

How do I get value of id=email and print it out to my console or anywhere for testing purposes ?

I tried, but no luck :(

<script type="text/javascript">
    document.getElementById('email').innerText;
</script>

Upvotes: 4

Views: 93839

Answers (9)

SaBah
SaBah

Reputation: 23

let inputId = document.querySelector("#email");
console.log(inputId.value);

Upvotes: 0

cjjenkinson
cjjenkinson

Reputation: 367

In the Chrome dev tools console drop the .value:

document.getElementById('#id')

Upvotes: 0

HumeNi
HumeNi

Reputation: 8738

If email(your id) is not defined, just:

email.innerHTML

enter image description here

Upvotes: -1

user4616043
user4616043

Reputation:

It works

console.log(email.value)

Upvotes: 0

Ekansh Rastogi
Ekansh Rastogi

Reputation: 2546

There are two types of elements

  1. Block - these can be accessed with - .innerHTML or .html() in jquery like div's span's etc
  2. Non Block Elements - these can be accessed by .value or .val() in jquery, like input types etc

all you need o do is

console.log($('#email').val())  //jquery

or 

console.log(document.getElementById('email').value); // javascript

Upvotes: 6

xphong
xphong

Reputation: 1114

Without jQuery:

console.log(document.getElementById('email').value);

Upvotes: 2

Brett Caswell
Brett Caswell

Reputation: 730

this will 'print' the entire object to the console

console.log("input[id='email'] - %o", document.getElementById('email')); 

Upvotes: 1

EricBellDesigns
EricBellDesigns

Reputation: 965

Try an alert...

http://jsfiddle.net/nemb666L/

var email = $("#email").val();
alert(email);

Upvotes: 1

Yosoyke
Yosoyke

Reputation: 485

try

console.log($('#email').val());

Upvotes: 3

Related Questions