0
var name = $("input[#name]").value;
var email = $("input[#email]").value;
var msg = $("input[#msg]").value;

var nameLength = name.length;
var emailLength = email.length;
var msgLength = msg.length;

As you can see, all I'm trying to do is to get the length of the value of the #email field. It works for both of the other fields, but not the email field. It give "TypeError: 'undefined' is not an object".

Any reason why?

I've tried in both Safari and Google Chrome, both gives the same error.

The HTML:

<div class="form-group">
  <label class="col-md-4 control-label" for="email">Email</label>  
  <div class="col-md-5">
    <input id="email" name="email" type="text" placeholder="Enter your email address
    here" class="form-control input-md" required="">
  </div>
</div>
user3666951
  • 23
  • 1
  • 1
  • 3
  • 5
    `.value` is not valid property of jQuery objects. You are looking for `.val()`: http://api.jquery.com/val/. See [Get value in input text box](http://stackoverflow.com/q/4088467/218196). – Felix Kling May 22 '14 at 22:19
  • Please show us the HTML you are trying to target so we can advise on whether your selectors are correct or not? – jfriend00 May 22 '14 at 22:34

1 Answers1

4

You are trying to use a DOM property on a jQuery object. If you have a jQuery object, then you must either use the jQuery method .val(), not the DOM property .value or you have to reach into the jQuery object to get the DOM object which you can then use DOM properties on.

I'd suggest you change it to use the jQuery method for getting the value:

var name = $("input[#name]").val();
var email = $("input[#email]").val();
var msg = $("input[#msg]").val();

Or, for illustrative purposes, this would also work (get the DOM object out of the jQuery object and use .value):

var name = $("input[#name]")[0].value;
var email = $("input[#email]")[0].value;
var msg = $("input[#msg]")[0].value;

Based on the HTML you've added to your question, you need to fix your selectors too. To target an id="xxx", you just use $("#xxx"). No other part of the selector is required because ids must be unique in the entire document. So, if all your HTML works like the email part you've shown in your question, then you can use this:

var name = $("#name").val();
var email = $("#email").val();
var msg = $("#msg").val();
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • That fixed that problem, however - it led to another problem further down in my code - giving me the same error on another variable. `var emailStructureAtPos = email.indexOf("@");` – user3666951 May 22 '14 at 22:24
  • @user3666951 - you're likely making some other mistake further down in your code. Not much we can do without seeing that code. – jfriend00 May 22 '14 at 22:25
  • @user3666951 - look in the debugger to see what `email` is at that time. Perhaps it has gone out of scope? – jfriend00 May 22 '14 at 22:28
  • @user3666951: Your selector looks rather strange. `input[#name]` selects an `input` which has an **attribute** named `#name`. I don't think `#` is even a valid character in attribute names, so I doubt this actually selects anything. If you want to select by ID, just use `#name`. I recommend to read the jQuery tutorial: http://learn.jquery.com/using-jquery-core/selecting-elements/ , http://learn.jquery.com/using-jquery-core/manipulating-elements/#getting-and-setting-information-about-elements . – Felix Kling May 22 '14 at 22:31
  • @FelixKling gives me same error wether it's `input[#email]` or just `#email` - or `input[name='email']` for that sake. – user3666951 May 22 '14 at 22:45
  • @user3666951: Then I don't know, but `input[#email]` looks definitely wrong. – Felix Kling May 22 '14 at 22:45
  • @user3666951 - show us the HTML (by using edit to add to the end of your question) you are targeting and we can help make sure the selector is correct. – jfriend00 May 22 '14 at 22:46
  • @user3666951 - Info added to my answer about fixing your selectors. – jfriend00 May 22 '14 at 22:52
  • @jfriend00 Yeah that fixed it, but brought up the original problem again. – user3666951 May 22 '14 at 22:57
  • 1
    @user3666951 - you need to learn some basic debugging techniques here. Examine the value of your variables and jQuery objects with either `console.log()` statements or in the debugger and figure out where things have gone wrong. You're obviously changing the code so I have no idea what error you're getting on what code now so there isn't much I can do based on your latest comment. You have errors in your code. Isolate the exact line of code, inspect the results of variables and expressions around that line of code and figure out what exactly isn't working or post a lot more specifics. – jfriend00 May 22 '14 at 23:01