Editing a date field based on the checkbox being checked












-1














I have multiple columns that are being displayed on the page. I wanted to add check boxes so the specified row can be edited based on selection. However there is a problem, because I am not able to edit the box that is checked.



For instance:



box       | ID | Date
checked | 1 | 11/20/18 <-- this should be editable
unchecked | 2 | 11/15/18 <-- this should just be displayed


JavaScript / Jquery:



  $(".id").on('click', function (e) {
var key = "#date-" + e.target.id;
var check = "#" + e.target.id;
if ($(check).attr(':checked')) {
$(key).removeAttr("readonly", false);
$(key).focus();
} else {
$(key).attr("readonly", "readonly");
}
});


HTML:



 @foreach (var item in Model)
{
<td><input class="id" type="checkbox" id=@item.ID/></td>
<td>@Html.DisplayFor(x => item.ID)</td>
<td><input id='@item.ID' type="text" value='@item.Date' readonly="readonly" /></td>
}


Any help would be appreciated!










share|improve this question
























  • stackoverflow.com/questions/9454645/… Don't repeat ids. Use a class instead
    – Taplar
    Nov 20 at 21:35










  • Uour have duplicate id attributes which is invalid html. And $("#date") selects the first element with id. Use a class name and relative selectors (relative to an enclosing parent element)
    – user3559349
    Nov 20 at 21:35










  • Refer How can i get h3 tag text value in jquery? for an example
    – user3559349
    Nov 20 at 21:37










  • @StephenMuecke tried that, but still stuck on the same issue.
    – caitlinp
    Nov 21 at 14:36










  • @caitlinp remove readonly="readonly". That will make your textbox exactly what it says (read only as in not editable).w3schools.com/tags/att_input_readonly.asp
    – Jabberwocky
    Nov 21 at 16:23


















-1














I have multiple columns that are being displayed on the page. I wanted to add check boxes so the specified row can be edited based on selection. However there is a problem, because I am not able to edit the box that is checked.



For instance:



box       | ID | Date
checked | 1 | 11/20/18 <-- this should be editable
unchecked | 2 | 11/15/18 <-- this should just be displayed


JavaScript / Jquery:



  $(".id").on('click', function (e) {
var key = "#date-" + e.target.id;
var check = "#" + e.target.id;
if ($(check).attr(':checked')) {
$(key).removeAttr("readonly", false);
$(key).focus();
} else {
$(key).attr("readonly", "readonly");
}
});


HTML:



 @foreach (var item in Model)
{
<td><input class="id" type="checkbox" id=@item.ID/></td>
<td>@Html.DisplayFor(x => item.ID)</td>
<td><input id='@item.ID' type="text" value='@item.Date' readonly="readonly" /></td>
}


Any help would be appreciated!










share|improve this question
























  • stackoverflow.com/questions/9454645/… Don't repeat ids. Use a class instead
    – Taplar
    Nov 20 at 21:35










  • Uour have duplicate id attributes which is invalid html. And $("#date") selects the first element with id. Use a class name and relative selectors (relative to an enclosing parent element)
    – user3559349
    Nov 20 at 21:35










  • Refer How can i get h3 tag text value in jquery? for an example
    – user3559349
    Nov 20 at 21:37










  • @StephenMuecke tried that, but still stuck on the same issue.
    – caitlinp
    Nov 21 at 14:36










  • @caitlinp remove readonly="readonly". That will make your textbox exactly what it says (read only as in not editable).w3schools.com/tags/att_input_readonly.asp
    – Jabberwocky
    Nov 21 at 16:23
















-1












-1








-1







I have multiple columns that are being displayed on the page. I wanted to add check boxes so the specified row can be edited based on selection. However there is a problem, because I am not able to edit the box that is checked.



For instance:



box       | ID | Date
checked | 1 | 11/20/18 <-- this should be editable
unchecked | 2 | 11/15/18 <-- this should just be displayed


JavaScript / Jquery:



  $(".id").on('click', function (e) {
var key = "#date-" + e.target.id;
var check = "#" + e.target.id;
if ($(check).attr(':checked')) {
$(key).removeAttr("readonly", false);
$(key).focus();
} else {
$(key).attr("readonly", "readonly");
}
});


HTML:



 @foreach (var item in Model)
{
<td><input class="id" type="checkbox" id=@item.ID/></td>
<td>@Html.DisplayFor(x => item.ID)</td>
<td><input id='@item.ID' type="text" value='@item.Date' readonly="readonly" /></td>
}


Any help would be appreciated!










share|improve this question















I have multiple columns that are being displayed on the page. I wanted to add check boxes so the specified row can be edited based on selection. However there is a problem, because I am not able to edit the box that is checked.



For instance:



box       | ID | Date
checked | 1 | 11/20/18 <-- this should be editable
unchecked | 2 | 11/15/18 <-- this should just be displayed


JavaScript / Jquery:



  $(".id").on('click', function (e) {
var key = "#date-" + e.target.id;
var check = "#" + e.target.id;
if ($(check).attr(':checked')) {
$(key).removeAttr("readonly", false);
$(key).focus();
} else {
$(key).attr("readonly", "readonly");
}
});


HTML:



 @foreach (var item in Model)
{
<td><input class="id" type="checkbox" id=@item.ID/></td>
<td>@Html.DisplayFor(x => item.ID)</td>
<td><input id='@item.ID' type="text" value='@item.Date' readonly="readonly" /></td>
}


Any help would be appreciated!







javascript jquery html asp.net-mvc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 19:00

























asked Nov 20 at 21:33









caitlinp

677




677












  • stackoverflow.com/questions/9454645/… Don't repeat ids. Use a class instead
    – Taplar
    Nov 20 at 21:35










  • Uour have duplicate id attributes which is invalid html. And $("#date") selects the first element with id. Use a class name and relative selectors (relative to an enclosing parent element)
    – user3559349
    Nov 20 at 21:35










  • Refer How can i get h3 tag text value in jquery? for an example
    – user3559349
    Nov 20 at 21:37










  • @StephenMuecke tried that, but still stuck on the same issue.
    – caitlinp
    Nov 21 at 14:36










  • @caitlinp remove readonly="readonly". That will make your textbox exactly what it says (read only as in not editable).w3schools.com/tags/att_input_readonly.asp
    – Jabberwocky
    Nov 21 at 16:23




















  • stackoverflow.com/questions/9454645/… Don't repeat ids. Use a class instead
    – Taplar
    Nov 20 at 21:35










  • Uour have duplicate id attributes which is invalid html. And $("#date") selects the first element with id. Use a class name and relative selectors (relative to an enclosing parent element)
    – user3559349
    Nov 20 at 21:35










  • Refer How can i get h3 tag text value in jquery? for an example
    – user3559349
    Nov 20 at 21:37










  • @StephenMuecke tried that, but still stuck on the same issue.
    – caitlinp
    Nov 21 at 14:36










  • @caitlinp remove readonly="readonly". That will make your textbox exactly what it says (read only as in not editable).w3schools.com/tags/att_input_readonly.asp
    – Jabberwocky
    Nov 21 at 16:23


















stackoverflow.com/questions/9454645/… Don't repeat ids. Use a class instead
– Taplar
Nov 20 at 21:35




stackoverflow.com/questions/9454645/… Don't repeat ids. Use a class instead
– Taplar
Nov 20 at 21:35












Uour have duplicate id attributes which is invalid html. And $("#date") selects the first element with id. Use a class name and relative selectors (relative to an enclosing parent element)
– user3559349
Nov 20 at 21:35




Uour have duplicate id attributes which is invalid html. And $("#date") selects the first element with id. Use a class name and relative selectors (relative to an enclosing parent element)
– user3559349
Nov 20 at 21:35












Refer How can i get h3 tag text value in jquery? for an example
– user3559349
Nov 20 at 21:37




Refer How can i get h3 tag text value in jquery? for an example
– user3559349
Nov 20 at 21:37












@StephenMuecke tried that, but still stuck on the same issue.
– caitlinp
Nov 21 at 14:36




@StephenMuecke tried that, but still stuck on the same issue.
– caitlinp
Nov 21 at 14:36












@caitlinp remove readonly="readonly". That will make your textbox exactly what it says (read only as in not editable).w3schools.com/tags/att_input_readonly.asp
– Jabberwocky
Nov 21 at 16:23






@caitlinp remove readonly="readonly". That will make your textbox exactly what it says (read only as in not editable).w3schools.com/tags/att_input_readonly.asp
– Jabberwocky
Nov 21 at 16:23














1 Answer
1






active

oldest

votes


















1














I could not test but, i thik this is very close answer to your approach...



using prefix and checkboxs' id together for identification,



 @foreach (var item in Model)
{
<td><input class="id" type="checkbox" id='@item.ID' /></td>
<td>@Html.DisplayFor(x => item.ID)</td>
<td><input id='date-@item.ID' type="text" value='@item.Date' readonly="readonly" /></td>
}


$(".id").on('click',function(e){
var key= "#date-"+ e.target.id;
if ($(key).attr("checked") == "checked") {
$(key).removeAttr("readonly", false);
$(key).focus();
} else {
$(key).attr("readonly", "readonly");
}
}
);





share|improve this answer





















  • Gives me the ID, but the textbox is still not editable..
    – caitlinp
    Nov 21 at 14:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401903%2fediting-a-date-field-based-on-the-checkbox-being-checked%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I could not test but, i thik this is very close answer to your approach...



using prefix and checkboxs' id together for identification,



 @foreach (var item in Model)
{
<td><input class="id" type="checkbox" id='@item.ID' /></td>
<td>@Html.DisplayFor(x => item.ID)</td>
<td><input id='date-@item.ID' type="text" value='@item.Date' readonly="readonly" /></td>
}


$(".id").on('click',function(e){
var key= "#date-"+ e.target.id;
if ($(key).attr("checked") == "checked") {
$(key).removeAttr("readonly", false);
$(key).focus();
} else {
$(key).attr("readonly", "readonly");
}
}
);





share|improve this answer





















  • Gives me the ID, but the textbox is still not editable..
    – caitlinp
    Nov 21 at 14:57
















1














I could not test but, i thik this is very close answer to your approach...



using prefix and checkboxs' id together for identification,



 @foreach (var item in Model)
{
<td><input class="id" type="checkbox" id='@item.ID' /></td>
<td>@Html.DisplayFor(x => item.ID)</td>
<td><input id='date-@item.ID' type="text" value='@item.Date' readonly="readonly" /></td>
}


$(".id").on('click',function(e){
var key= "#date-"+ e.target.id;
if ($(key).attr("checked") == "checked") {
$(key).removeAttr("readonly", false);
$(key).focus();
} else {
$(key).attr("readonly", "readonly");
}
}
);





share|improve this answer





















  • Gives me the ID, but the textbox is still not editable..
    – caitlinp
    Nov 21 at 14:57














1












1








1






I could not test but, i thik this is very close answer to your approach...



using prefix and checkboxs' id together for identification,



 @foreach (var item in Model)
{
<td><input class="id" type="checkbox" id='@item.ID' /></td>
<td>@Html.DisplayFor(x => item.ID)</td>
<td><input id='date-@item.ID' type="text" value='@item.Date' readonly="readonly" /></td>
}


$(".id").on('click',function(e){
var key= "#date-"+ e.target.id;
if ($(key).attr("checked") == "checked") {
$(key).removeAttr("readonly", false);
$(key).focus();
} else {
$(key).attr("readonly", "readonly");
}
}
);





share|improve this answer












I could not test but, i thik this is very close answer to your approach...



using prefix and checkboxs' id together for identification,



 @foreach (var item in Model)
{
<td><input class="id" type="checkbox" id='@item.ID' /></td>
<td>@Html.DisplayFor(x => item.ID)</td>
<td><input id='date-@item.ID' type="text" value='@item.Date' readonly="readonly" /></td>
}


$(".id").on('click',function(e){
var key= "#date-"+ e.target.id;
if ($(key).attr("checked") == "checked") {
$(key).removeAttr("readonly", false);
$(key).focus();
} else {
$(key).attr("readonly", "readonly");
}
}
);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 21:53









kanpinar

1228




1228












  • Gives me the ID, but the textbox is still not editable..
    – caitlinp
    Nov 21 at 14:57


















  • Gives me the ID, but the textbox is still not editable..
    – caitlinp
    Nov 21 at 14:57
















Gives me the ID, but the textbox is still not editable..
– caitlinp
Nov 21 at 14:57




Gives me the ID, but the textbox is still not editable..
– caitlinp
Nov 21 at 14:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401903%2fediting-a-date-field-based-on-the-checkbox-being-checked%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python