Editing a date field based on the checkbox being checked
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
|
show 7 more comments
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
stackoverflow.com/questions/9454645/… Don't repeat ids. Use a class instead
– Taplar
Nov 20 at 21:35
Uour have duplicateid
attributes which is invalid html. And$("#date")
selects the first element withid
. 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
|
show 7 more comments
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
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
javascript jquery html asp.net-mvc
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 duplicateid
attributes which is invalid html. And$("#date")
selects the first element withid
. 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
|
show 7 more comments
stackoverflow.com/questions/9454645/… Don't repeat ids. Use a class instead
– Taplar
Nov 20 at 21:35
Uour have duplicateid
attributes which is invalid html. And$("#date")
selects the first element withid
. 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
|
show 7 more comments
1 Answer
1
active
oldest
votes
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");
}
}
);
Gives me the ID, but the textbox is still not editable..
– caitlinp
Nov 21 at 14:57
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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");
}
}
);
Gives me the ID, but the textbox is still not editable..
– caitlinp
Nov 21 at 14:57
add a comment |
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");
}
}
);
Gives me the ID, but the textbox is still not editable..
– caitlinp
Nov 21 at 14:57
add a comment |
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");
}
}
);
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");
}
}
);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 withid
. 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