I am trying to pass a value from a hidden input using ajax but console says whatever im doing is not a function

Uncaught TypeError: "#name".val is not a function page.php:35

which is this line : var name = ("#name").val();

html:

<input type='hidden' value='$newname' id='name'>

ajax:

var name = ("#name").val();

$.ajax({
    type: "POST",
    url: "declineRequest.php",
    data: "name="+name,
    success:  function(data){
      alert("Request Declined!");
    }
});

why does the error occur? what did i do wrong? tq.

var name = $("#name").val();
Member Avatar

diafol

Hmm. Does 'name' as a varname cause issues? I think I ran into this a while ago.

i changed it multiple times so its not name = name but i still get the same error in the console.

var full = ("#fullname").val();

Uncaught TypeError: "#fullname".val is not a function

You're using jQuery... so you have to call the jQuery library using $ or jQuery like $("#fullname").val() or jQuery("#fullname").val().

Also, where is the JavaScript code at on the page? Did you put in in the head of the page, or towards the bottom of the body?
If the JavaScript runs before the elements have been loaded, then you'll run into problems.
If you want to keep it in the head, you'll need to tell it to wait until the DOM has finished loading.

$(document).ready(function(){

    // your code would go here

});

oh ya! crap. after looking at the code for like ever. totally didn't realize that. thank you!pixelsoul and pritaeas