Why is this script returning an object to me?

Asked

Viewed 60 times

2

I am working with jQuery to register information in the database through Ajax and do not know why this code is returning me a variable of the object type. The project I’m working on is being developed on top of a Admin Panel with the entire interface and HTML, CSS e JS ready. Here is the code:

$("#cadastra_produtos").submit(function(){

    var prod_nome       = $("#prod_nome");
    var prod_categoria  = $("#prod_categoria");
    var prod_dimens     = $("#prod_dimens");
    var prod_qtde       = $("#prod_qtde");
    var prod_valor      = $("#prod_valor");
    var prod_descr      = $("#prod_descr");

    if(prod_nome) { alert(prod_nome); } <-- isso me retorna um objeto

The only particularity of this form that I think could be generating some type error is the plugin chosen-select that is part of the system front-end.

You can help me figure out why Alert shows me an object and not the string?

  • 1

    Missed catching the .val() of all fields. The object you are seeing is the jQuery object that wraps the field in question.

  • Note: if(prod_nome) never will fall into the else.

  • Nooooooooooooooooossa forgot the val()

1 answer

6


It seems you’ve already noticed but for didactic purposes..

in the example:

html

<input id="prod_name" />

javascript

var nome = $("#prod_nome");

name receives an encapsulated object by jQuery to manipulate the DOM object representing the <input /> in Javascript logo to access its value must use the .val() getting

var nome = $("#prod_nome").val();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.