views:

192

answers:

1

The following code produces

function (value) { if (value === undefined) { var elem = this[0]; if (elem) { if (jQuery.nodeName(elem, "option")) { return (elem.attributes.value || {}).specified ? elem.value : elem.text; } if (jQuery.nodeName(elem, "select")) { var index = elem.selectedIndex, values = [], options = elem.options, one = elem.type == "select-one"; if (index < 0) { return null; } for (var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++) { var option = options[i]; if (option.selected) { value = jQuery(option).val(); if (one) { return value; } values.push(value); } } return values; } return (elem.value || "").replace(/\r/g, ""); } return undefined; } if (typeof value === "number") { value += ""; } return this.each(function () {if (this.nodeType != 1) {return;}if (jQuery.isArray(value) && /radio|checkbox/.test(this.type)) {this.checked = jQuery.inArray(this.value, value) >= 0 || jQuery.inArray(this.name, value) >= 0;} else if (jQuery.nodeName(this, "select")) {var values = jQuery.makeArray(value);jQuery("option", this).each(function () {this.selected = jQuery.inArray(this.value, values) >= 0 || jQuery.inArray(this.text, values) >= 0;});if (!values.length) {this.selectedIndex = -1;}} else {this.value = value;}}); }

when the alert is called. I was expecting to see just "Hello". For whatever reason I can get the jQuery event to fire using the selector, but when I try and use any of the DOM elements inside that event it fails. Any thoughts?

<script type="text/javascript">
    $(function() {


        $("#myButton").click(function() { alert("clicked!"); alert($("#myHTML").val ); });


    }
);

<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc&lt;/a&gt;.
</p>
<button id="myButton" style="height:50px; width:200px;" >Click Me!</button>
<div id="myHTML">
hello
</div>

+4  A: 

$("#myHTML").val() should work. Make sure you include the parenthesis.

Also, for alerting the contents of a DIV, .text() or .html() would be better.

Jonathan Sampson
:) Thanks much. Pretty weak for my first question of stackoverflow! The VB in me caused me to miss the parens.
vonfeldj