How do I access the value of a variable set in razor from jQuery? In the code below, I'm expecting "admin" but am getting "object Object" instead. Something fundamental I'm missing.
2 Answers
Never use alert(object) to try to debug. Use
console.log("_area:", "@_area", $("@_area").length)
Objects always appears as [Object object] when coerced to a string
(either via alert or via "" + obj)
In your case, @_area = "admin" so this will output in the html/javascript as:
$("admin")
which isn't a valid selector, so should be:
$("#@_area")
Also note that if you output a jquery object $("bad_selector") - it's still a jquery object, so use $("bad_selector").length to determine if it's working.
You can also view the rendered html to see what your Razor is generating, sometimes this will make the answer obvious.
Comments
Because $('...') - is JQuery object. Check this out: alert("_area: " + '@_area')
1 Comment
alert('_area: @_area')
