0

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?

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">

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


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


        }
    );
</script>

    <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</a>.
    </p>
    <button id="myButton" style="height:50px; width:200px;" >Click Me!</button>
    <div id="myHTML">
    hello
    </div>
</asp:Content>

1 Answer 1

4

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

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.