1

All I'm trying to do is get the val() of an Text Input. Here is an example.

I don't know if external links are allowed so I will also paste it here :

<div class="input-group">
  <input type="text" id="#test" class="form-control" aria-label="..." style='border-radius:4px;'>
</div>

<script>
  // Always shows up as undefined..not "".
  console.log($("#test").val());
</script>

Does anyone know why I cannot get the value of this element?

1 Answer 1

2

You have to remove the # sign from the id attribute :

<input type="text" id="#test" class="form-control" aria-label="..."  ..
//_____________________^

Hope this helps.


<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Why can't I get the input value of the #test?</title>
</head>
<body style='padding:64px;'>

  <div class="input-group">
    <input type="text" value="test value" id="test" class="form-control" aria-label="..." style='border-radius:4px;'>
  </div>
  
  <script>
    // Always shows up as undefined..not "".
    console.log($("#test").val());
  </script>
  
</body>
</html>

Sign up to request clarification or add additional context in comments.

1 Comment

Ah.. :( this is what happens when you stay up too long. Thanks though, for awhile there I thought it was something to do with the shadow element beneath the input.

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.