{"id":429,"date":"2022-08-30T15:01:12","date_gmt":"2022-08-30T15:01:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/why-does-val-returns-undefined-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:12","modified_gmt":"2022-08-30T15:01:12","slug":"why-does-val-returns-undefined-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-does-val-returns-undefined-collection-of-common-programming-errors\/","title":{"rendered":"Why does val() returns undefined?-Collection of common programming errors"},"content":{"rendered":"<p>i try to get the value of the next hidden input field when a button is clicked but my script always returns &#8220;undefined&#8221;. What am i doing wrong and why does val() returns undefined? If only try to select the element with next(&#8216;input:hidden&#8217;) the function returns an object.<\/p>\n<pre><code>        \n            \n                \n                    <\/code><\/pre>\n<ul class=\"unstyled inline\">\n<li><code>Er will dich 1<\/code><\/li>\n<li><code>Er will dich nicht 5<\/code><\/li>\n<li><code>Es ist alles offen 2<\/code><\/li>\n<\/ul>\n<pre>\n                    \n                \n            \n        \n        \n            \n                \n                    <\/pre>\n<ul class=\"unstyled inline\">\n<li><code>Er will dich 1<\/code><\/li>\n<li><code>Er will dich nicht 5<\/code><\/li>\n<li><code>Es ist alles offen 2<\/code><\/li>\n<\/ul>\n<pre>\n                    \n                \n            \n        \n<\/pre>\n<p><code><em>JavaScript:<\/em><\/code><\/p>\n<pre><code><code>$(document).ready(function() {\n    $('button.rating-button').click(function(event) {\n        event.preventDefault();\n        var $rating = $(this).attr('value');\n        var $messageId = $(this).next('input:hidden').val();\n\n        alert('test'+$messageId);\n    });\n});\n<\/code><\/code><\/pre>\n<ol>\n<li>\n<p>It happens because there is no <code>next<\/code> element for . You have to go up the DOM tree until and find the hidden element:<\/p>\n<pre><code>var $messageId = $(this).closest(\".message-ratebox\").find(\"input:hidden\").val();\n<\/code><\/pre>\n<p>or to go up until<\/p>\n<ul>and select the <code>next<\/code>element:<\/p>\n<pre><code>var $messageId = $(this).closest(\"ul\").next(\"input:hidden\").val();\n<\/code><\/pre>\n<\/ul>\n<\/li>\n<li>\n<pre><code>        $('button.rating-button').click(function(event) {\n            event.preventDefault();\n            var $rating = $(this).val();\n            var $messageId = $(this).parent().parent().parent().find('input').val();\n            alert($rating + $messageId );\n\n           });\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You need to parse back up from the button to the input&#8217;s container, which in this case is the nearest div, and then parse back down to the hidden input&#8230;<\/p>\n<pre><code>$('button.rating-button').click(function(event) {\n    event.preventDefault();\n    var $rating = $(this).val();\n    var $messageId = $(this).closest(\"div\").find('input[type=hidden]').val();\n    alert($rating + $messageId);\n});\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:41:39. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>i try to get the value of the next hidden input field when a button is clicked but my script always returns &#8220;undefined&#8221;. What am i doing wrong and why does val() returns undefined? If only try to select the element with next(&#8216;input:hidden&#8217;) the function returns an object. Er will dich 1 Er will dich [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-429","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=429"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/429\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}