0

This code prints about 15 rows of data with this select box near each row. I need to capture user's selected value for each row to send it of to the next function. I'm receiving this error: Uncaught TypeError: Object # has no method 'val'. Is there a better way to achieve this?

count = 1;
$.each(data, function(index, element) {
    $('<div class="hd_field">Payment Format: <select name="Payment_Format" id="Payment' + count + '" class="Payment_Format"><option value="CTX" selected="selected">Company to Company</option><option value="PPD">Company to Person</option></select><div id ="Selected_Format' + count + '"></div></div>').appendTo(newDiv3);
    $('select#Payment' + count).change(function() {
        alert(this.val());
        PaymentFormat = $(this).val();
        element.PmtRec.PmtFormat = PaymentFormat;
        $('#Selected_Format' + count).text(PaymentFormat);
    });
    console.log(data);
    count = count + 1;
});​
2
  • Is the data variable an array? Commented Jun 6, 2012 at 15:48
  • Yes, its a multidimensional array. Commented Jun 6, 2012 at 16:19

1 Answer 1

1

Your error is thrown on this line:

$('select#Payment'+count).change(function() { 
    alert(this.val()); //Here
    //...
});

The problem is that in the change event handler, this refers to the actual DOM node, not a jQuery object, and the DOM node will not have a val method. You need to pass it to jQuery if you want to use val:

$('select#Payment'+count).change(function() {
    alert($(this).val()); //Pass `this` to jQuery
    //...
});
Sign up to request clarification or add additional context in comments.

3 Comments

or change to alert(this.value)
Thank you that worked! But, the alert was just a test to see if the .change is working, the .text is still not doing anything.
Also, the object assignment isn't changing every time I select a different option.

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.