I have the following jquery code :
function InitVoteContainer(voteContainer) {
var _userVote = voteContainer.children(".storeUserVote").val();
var _userObjectId = voteContainer.children(".storeObjectId").val();
var _userVoteKey = voteContainer.children(".storeVoteKey").val();
var _UserAuth = voteContainer.children(".storeUserAuth").val();
var _voteButtonUp = voteContainer.children("div[pid='btUp']");
var _voteButtonDown = voteContainer.children("div[pid='btDown']");
var upBtValue = 0;
var downBtValue = 0;
_voteButtonUp.attr("class", "upVote_inactive");
_voteButtonDown.attr("class", "downVote_inactive");
_voteButtonUp.prop("onclick", null);
_voteButtonDown.prop("onclick", null);
}
The code will switch class on the upVote and downVote buttons. The code runs fine but the classes is never set? the question is why??
Edit 1:
Updates with some more code :
This jquery code runnes at dodument ready
function InitVoteControls() {
$("#mainPostList").find(".voteCon").each(function () {
InitVoteContainer($(this));
});
}
And this is the HTML that the jquery is using :
<ul id="mainPostList" class="verticalList">
@foreach (var postViewModel in Model.Posts)
{
<li>
<div class="postContainer">
<div class="voteCon">
<input class="storeUserVote" type="hidden" value="@Model.UserVote" />
<input class="storeObjectId" type="hidden" value="@Model.ObjectId" />
<input class="storeVoteKey" type="hidden" value="@Model.VoteKey" />
<input class="storeUserAuth" type="hidden" value="@Request.IsAuthenticated" />
@if (!Request.IsAuthenticated)
{
<div pid="btUpVote" class="upVote"></div>
}
else
{
<div pid="btUpVote" class="upVote_inactive"></div>
}
<br style="clear:both;" />
<div class="voteCountBox">
@Html.Encode(Model.VoteBalance)
</div>
<br style="clear:both;" />
@if (!Request.IsAuthenticated)
{
<div pid="btDownVote" class="downVote"></div>
}
else
{
<div pid="btDownVote" class="downVote_inactive"></div>
}
</div>
</div>
</li>
}
</ul>
In your JavaScript you are selecting your button with
which will return an emtpy jQuery object, since you have no div in your markup with the exact attribute
pid='btUp'. You should usepid^='btUp'instead – or directlypid='btUpVote'.See this example or the W3C specification.