javascript - Cannot Read Property of Undefined JQuery Option Tag -
i getting error cannot read property '####' of undefined (#### being number attempt use)
the goal retrieve inner html/text of form options based on number provide. here example of text trying retrieve using jquery can send form data.
<select> <option value="1">sword</option> </select>
in example, trying retrieve text -> "sword" based on option value "1". how attempt this:
$item = $_post['item']; $option = "option[value='".$item."']";
$_post['item'] returns number (in example, "1") use retrieve inner text of option given value.
the following directly after 2 lines above, attempts set jquery variable option.
echo '<script type="text/javascript">var option = $(option[value="'.$item.'"]).html();</script>';
lastly jquery code, uses $option variable initialized earlier attempt retrieve text inside option ("sword")
<script> console.log(option); var itemname = $(this.option).html(); $.ajax({ type: "post", data:{ itemname: itemname }, success: function(data){ console.log(itemname); } }) </script>
i know code wouldn't work, because using console.log log item name. however, problem console log:
cannot read property '1' of undefined.
any advice? lot, appreciated!
the correct way access option's text jquery following:
$('option[value="1"]').text()
this return sword
in example. important difference using .text()
vs .html()
.
Comments
Post a Comment