Set visibility on/off on button in table












0















I am creating a table with a button in the 5th column. The button shall first be visible when the user clicks on the row of the table. I am having truble setting visibility on....



Here is my code:



for (var i = dataRaw.length; i > 0; i--) {

var r = dataRaw[i-1];
var row = table.insertRow(0);
row.id = r[0];
for (var x = 0; x < r.length; x++) {

if (i === 1) {
var headerCell = document.createElement("TH");
headerCell.innerHTML = r[x];
row.appendChild(headerCell);

} else {
var cell = row.insertCell(x);

if (x === 5) {

var btn = document.createElement('input');
btn.type = "button";
btn.className = "btn";
btn.id = r[0].toString();
btn.value = "Vis";
btn.style.backgroundColor = '#428bca';
btn.style.color = 'white';
btn.style.visibility = "hidden";
cell.appendChild(btn);

}
else {
cell.innerHTML = r[x];
}

}
}
}


I have this listener on the table :



    $(function () {
$('#errorTable').on('click', 'tr', function () {

var requestID = this.id;

document.getElementById(requestID).style.visibility = 'visible';


The button is "hidden" at start, but I cannot show it again.....Anyone ?










share|improve this question

























  • Please show your HTML snippet, and reduce the Javascript to only the parts that matter to resolve your issue

    – Ahmad
    Nov 22 '18 at 10:34











  • If I use style.display = 'none', the whole row is hidden.....Only want to hid button in the row

    – Joe Doe
    Nov 22 '18 at 10:35











  • Then you should simply use $(this).find('button').first().hide();

    – Ahmad
    Nov 22 '18 at 10:35













  • Looks like you are using the same ID value for the TR, and for the button element? IDs must be unique within an HTML document.

    – misorude
    Nov 22 '18 at 10:36
















0















I am creating a table with a button in the 5th column. The button shall first be visible when the user clicks on the row of the table. I am having truble setting visibility on....



Here is my code:



for (var i = dataRaw.length; i > 0; i--) {

var r = dataRaw[i-1];
var row = table.insertRow(0);
row.id = r[0];
for (var x = 0; x < r.length; x++) {

if (i === 1) {
var headerCell = document.createElement("TH");
headerCell.innerHTML = r[x];
row.appendChild(headerCell);

} else {
var cell = row.insertCell(x);

if (x === 5) {

var btn = document.createElement('input');
btn.type = "button";
btn.className = "btn";
btn.id = r[0].toString();
btn.value = "Vis";
btn.style.backgroundColor = '#428bca';
btn.style.color = 'white';
btn.style.visibility = "hidden";
cell.appendChild(btn);

}
else {
cell.innerHTML = r[x];
}

}
}
}


I have this listener on the table :



    $(function () {
$('#errorTable').on('click', 'tr', function () {

var requestID = this.id;

document.getElementById(requestID).style.visibility = 'visible';


The button is "hidden" at start, but I cannot show it again.....Anyone ?










share|improve this question

























  • Please show your HTML snippet, and reduce the Javascript to only the parts that matter to resolve your issue

    – Ahmad
    Nov 22 '18 at 10:34











  • If I use style.display = 'none', the whole row is hidden.....Only want to hid button in the row

    – Joe Doe
    Nov 22 '18 at 10:35











  • Then you should simply use $(this).find('button').first().hide();

    – Ahmad
    Nov 22 '18 at 10:35













  • Looks like you are using the same ID value for the TR, and for the button element? IDs must be unique within an HTML document.

    – misorude
    Nov 22 '18 at 10:36














0












0








0








I am creating a table with a button in the 5th column. The button shall first be visible when the user clicks on the row of the table. I am having truble setting visibility on....



Here is my code:



for (var i = dataRaw.length; i > 0; i--) {

var r = dataRaw[i-1];
var row = table.insertRow(0);
row.id = r[0];
for (var x = 0; x < r.length; x++) {

if (i === 1) {
var headerCell = document.createElement("TH");
headerCell.innerHTML = r[x];
row.appendChild(headerCell);

} else {
var cell = row.insertCell(x);

if (x === 5) {

var btn = document.createElement('input');
btn.type = "button";
btn.className = "btn";
btn.id = r[0].toString();
btn.value = "Vis";
btn.style.backgroundColor = '#428bca';
btn.style.color = 'white';
btn.style.visibility = "hidden";
cell.appendChild(btn);

}
else {
cell.innerHTML = r[x];
}

}
}
}


I have this listener on the table :



    $(function () {
$('#errorTable').on('click', 'tr', function () {

var requestID = this.id;

document.getElementById(requestID).style.visibility = 'visible';


The button is "hidden" at start, but I cannot show it again.....Anyone ?










share|improve this question
















I am creating a table with a button in the 5th column. The button shall first be visible when the user clicks on the row of the table. I am having truble setting visibility on....



Here is my code:



for (var i = dataRaw.length; i > 0; i--) {

var r = dataRaw[i-1];
var row = table.insertRow(0);
row.id = r[0];
for (var x = 0; x < r.length; x++) {

if (i === 1) {
var headerCell = document.createElement("TH");
headerCell.innerHTML = r[x];
row.appendChild(headerCell);

} else {
var cell = row.insertCell(x);

if (x === 5) {

var btn = document.createElement('input');
btn.type = "button";
btn.className = "btn";
btn.id = r[0].toString();
btn.value = "Vis";
btn.style.backgroundColor = '#428bca';
btn.style.color = 'white';
btn.style.visibility = "hidden";
cell.appendChild(btn);

}
else {
cell.innerHTML = r[x];
}

}
}
}


I have this listener on the table :



    $(function () {
$('#errorTable').on('click', 'tr', function () {

var requestID = this.id;

document.getElementById(requestID).style.visibility = 'visible';


The button is "hidden" at start, but I cannot show it again.....Anyone ?







javascript html-table visibility






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 10:37







Joe Doe

















asked Nov 22 '18 at 10:32









Joe DoeJoe Doe

687




687













  • Please show your HTML snippet, and reduce the Javascript to only the parts that matter to resolve your issue

    – Ahmad
    Nov 22 '18 at 10:34











  • If I use style.display = 'none', the whole row is hidden.....Only want to hid button in the row

    – Joe Doe
    Nov 22 '18 at 10:35











  • Then you should simply use $(this).find('button').first().hide();

    – Ahmad
    Nov 22 '18 at 10:35













  • Looks like you are using the same ID value for the TR, and for the button element? IDs must be unique within an HTML document.

    – misorude
    Nov 22 '18 at 10:36



















  • Please show your HTML snippet, and reduce the Javascript to only the parts that matter to resolve your issue

    – Ahmad
    Nov 22 '18 at 10:34











  • If I use style.display = 'none', the whole row is hidden.....Only want to hid button in the row

    – Joe Doe
    Nov 22 '18 at 10:35











  • Then you should simply use $(this).find('button').first().hide();

    – Ahmad
    Nov 22 '18 at 10:35













  • Looks like you are using the same ID value for the TR, and for the button element? IDs must be unique within an HTML document.

    – misorude
    Nov 22 '18 at 10:36

















Please show your HTML snippet, and reduce the Javascript to only the parts that matter to resolve your issue

– Ahmad
Nov 22 '18 at 10:34





Please show your HTML snippet, and reduce the Javascript to only the parts that matter to resolve your issue

– Ahmad
Nov 22 '18 at 10:34













If I use style.display = 'none', the whole row is hidden.....Only want to hid button in the row

– Joe Doe
Nov 22 '18 at 10:35





If I use style.display = 'none', the whole row is hidden.....Only want to hid button in the row

– Joe Doe
Nov 22 '18 at 10:35













Then you should simply use $(this).find('button').first().hide();

– Ahmad
Nov 22 '18 at 10:35







Then you should simply use $(this).find('button').first().hide();

– Ahmad
Nov 22 '18 at 10:35















Looks like you are using the same ID value for the TR, and for the button element? IDs must be unique within an HTML document.

– misorude
Nov 22 '18 at 10:36





Looks like you are using the same ID value for the TR, and for the button element? IDs must be unique within an HTML document.

– misorude
Nov 22 '18 at 10:36












1 Answer
1






active

oldest

votes


















0














Assuming each row has a single button, or you want to hide the first button inside the row



$('#errorTable').on('click', 'tr', function () {

//document.getElementById(requestID).style.visibility = 'visible';

$(this).find('button').first().hide();
// or if you set your buttons as INPUTS:
$(this).find('input[type=button]').first().hide();
}


notice: this method does not require the unique id of the row that was clicked. Unless you want to relay it back to the server.



Edit



your code that generates the button also can use some modification:



if (x === 5) {
var btn = $('<input>')
.attr("type","button")
.attr("id",r[0].toString())
.attr("value","Vis")
.css("background-color","#428bca")
.css("color","white")
.css("display","none") // initially hidden
.addClass("btn");

btn.appendTo($(cell));
}


Edit 2



To toggle visiblity of the button again, as you described in the comment, there is no need to invoke .first() here, since there is only one element with that ID.



$("#" +requestID+ "btn").show();





share|improve this answer


























  • Works fine :) But, if I first want to hide then show, it does not work.... First I set: btn.style.visibility = "hidden"; then I tried $(this).find('button').first().show(); and $(this).find('input[type=button]').first().show(); .......but not working

    – Joe Doe
    Nov 22 '18 at 10:52











  • @JoeDoe I edited my answer to accommodate your comment

    – Ahmad
    Nov 22 '18 at 10:59











  • your modification works, but messes other elements in site. Can't I use my version of the button, and use $(this).find to find button by id ? If I add btn.id = r[0].toString()+"btn" then each button has unique id different from tablerow id... ??

    – Joe Doe
    Nov 22 '18 at 11:11













  • Something like: $(requestID+"btn").first().show();

    – Joe Doe
    Nov 22 '18 at 11:14











  • @JoeDoe I edited my answer again to show how you might show the button again

    – Ahmad
    Nov 22 '18 at 11:22











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%2f53428932%2fset-visibility-on-off-on-button-in-table%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









0














Assuming each row has a single button, or you want to hide the first button inside the row



$('#errorTable').on('click', 'tr', function () {

//document.getElementById(requestID).style.visibility = 'visible';

$(this).find('button').first().hide();
// or if you set your buttons as INPUTS:
$(this).find('input[type=button]').first().hide();
}


notice: this method does not require the unique id of the row that was clicked. Unless you want to relay it back to the server.



Edit



your code that generates the button also can use some modification:



if (x === 5) {
var btn = $('<input>')
.attr("type","button")
.attr("id",r[0].toString())
.attr("value","Vis")
.css("background-color","#428bca")
.css("color","white")
.css("display","none") // initially hidden
.addClass("btn");

btn.appendTo($(cell));
}


Edit 2



To toggle visiblity of the button again, as you described in the comment, there is no need to invoke .first() here, since there is only one element with that ID.



$("#" +requestID+ "btn").show();





share|improve this answer


























  • Works fine :) But, if I first want to hide then show, it does not work.... First I set: btn.style.visibility = "hidden"; then I tried $(this).find('button').first().show(); and $(this).find('input[type=button]').first().show(); .......but not working

    – Joe Doe
    Nov 22 '18 at 10:52











  • @JoeDoe I edited my answer to accommodate your comment

    – Ahmad
    Nov 22 '18 at 10:59











  • your modification works, but messes other elements in site. Can't I use my version of the button, and use $(this).find to find button by id ? If I add btn.id = r[0].toString()+"btn" then each button has unique id different from tablerow id... ??

    – Joe Doe
    Nov 22 '18 at 11:11













  • Something like: $(requestID+"btn").first().show();

    – Joe Doe
    Nov 22 '18 at 11:14











  • @JoeDoe I edited my answer again to show how you might show the button again

    – Ahmad
    Nov 22 '18 at 11:22
















0














Assuming each row has a single button, or you want to hide the first button inside the row



$('#errorTable').on('click', 'tr', function () {

//document.getElementById(requestID).style.visibility = 'visible';

$(this).find('button').first().hide();
// or if you set your buttons as INPUTS:
$(this).find('input[type=button]').first().hide();
}


notice: this method does not require the unique id of the row that was clicked. Unless you want to relay it back to the server.



Edit



your code that generates the button also can use some modification:



if (x === 5) {
var btn = $('<input>')
.attr("type","button")
.attr("id",r[0].toString())
.attr("value","Vis")
.css("background-color","#428bca")
.css("color","white")
.css("display","none") // initially hidden
.addClass("btn");

btn.appendTo($(cell));
}


Edit 2



To toggle visiblity of the button again, as you described in the comment, there is no need to invoke .first() here, since there is only one element with that ID.



$("#" +requestID+ "btn").show();





share|improve this answer


























  • Works fine :) But, if I first want to hide then show, it does not work.... First I set: btn.style.visibility = "hidden"; then I tried $(this).find('button').first().show(); and $(this).find('input[type=button]').first().show(); .......but not working

    – Joe Doe
    Nov 22 '18 at 10:52











  • @JoeDoe I edited my answer to accommodate your comment

    – Ahmad
    Nov 22 '18 at 10:59











  • your modification works, but messes other elements in site. Can't I use my version of the button, and use $(this).find to find button by id ? If I add btn.id = r[0].toString()+"btn" then each button has unique id different from tablerow id... ??

    – Joe Doe
    Nov 22 '18 at 11:11













  • Something like: $(requestID+"btn").first().show();

    – Joe Doe
    Nov 22 '18 at 11:14











  • @JoeDoe I edited my answer again to show how you might show the button again

    – Ahmad
    Nov 22 '18 at 11:22














0












0








0







Assuming each row has a single button, or you want to hide the first button inside the row



$('#errorTable').on('click', 'tr', function () {

//document.getElementById(requestID).style.visibility = 'visible';

$(this).find('button').first().hide();
// or if you set your buttons as INPUTS:
$(this).find('input[type=button]').first().hide();
}


notice: this method does not require the unique id of the row that was clicked. Unless you want to relay it back to the server.



Edit



your code that generates the button also can use some modification:



if (x === 5) {
var btn = $('<input>')
.attr("type","button")
.attr("id",r[0].toString())
.attr("value","Vis")
.css("background-color","#428bca")
.css("color","white")
.css("display","none") // initially hidden
.addClass("btn");

btn.appendTo($(cell));
}


Edit 2



To toggle visiblity of the button again, as you described in the comment, there is no need to invoke .first() here, since there is only one element with that ID.



$("#" +requestID+ "btn").show();





share|improve this answer















Assuming each row has a single button, or you want to hide the first button inside the row



$('#errorTable').on('click', 'tr', function () {

//document.getElementById(requestID).style.visibility = 'visible';

$(this).find('button').first().hide();
// or if you set your buttons as INPUTS:
$(this).find('input[type=button]').first().hide();
}


notice: this method does not require the unique id of the row that was clicked. Unless you want to relay it back to the server.



Edit



your code that generates the button also can use some modification:



if (x === 5) {
var btn = $('<input>')
.attr("type","button")
.attr("id",r[0].toString())
.attr("value","Vis")
.css("background-color","#428bca")
.css("color","white")
.css("display","none") // initially hidden
.addClass("btn");

btn.appendTo($(cell));
}


Edit 2



To toggle visiblity of the button again, as you described in the comment, there is no need to invoke .first() here, since there is only one element with that ID.



$("#" +requestID+ "btn").show();






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 11:22

























answered Nov 22 '18 at 10:39









AhmadAhmad

8,20543463




8,20543463













  • Works fine :) But, if I first want to hide then show, it does not work.... First I set: btn.style.visibility = "hidden"; then I tried $(this).find('button').first().show(); and $(this).find('input[type=button]').first().show(); .......but not working

    – Joe Doe
    Nov 22 '18 at 10:52











  • @JoeDoe I edited my answer to accommodate your comment

    – Ahmad
    Nov 22 '18 at 10:59











  • your modification works, but messes other elements in site. Can't I use my version of the button, and use $(this).find to find button by id ? If I add btn.id = r[0].toString()+"btn" then each button has unique id different from tablerow id... ??

    – Joe Doe
    Nov 22 '18 at 11:11













  • Something like: $(requestID+"btn").first().show();

    – Joe Doe
    Nov 22 '18 at 11:14











  • @JoeDoe I edited my answer again to show how you might show the button again

    – Ahmad
    Nov 22 '18 at 11:22



















  • Works fine :) But, if I first want to hide then show, it does not work.... First I set: btn.style.visibility = "hidden"; then I tried $(this).find('button').first().show(); and $(this).find('input[type=button]').first().show(); .......but not working

    – Joe Doe
    Nov 22 '18 at 10:52











  • @JoeDoe I edited my answer to accommodate your comment

    – Ahmad
    Nov 22 '18 at 10:59











  • your modification works, but messes other elements in site. Can't I use my version of the button, and use $(this).find to find button by id ? If I add btn.id = r[0].toString()+"btn" then each button has unique id different from tablerow id... ??

    – Joe Doe
    Nov 22 '18 at 11:11













  • Something like: $(requestID+"btn").first().show();

    – Joe Doe
    Nov 22 '18 at 11:14











  • @JoeDoe I edited my answer again to show how you might show the button again

    – Ahmad
    Nov 22 '18 at 11:22

















Works fine :) But, if I first want to hide then show, it does not work.... First I set: btn.style.visibility = "hidden"; then I tried $(this).find('button').first().show(); and $(this).find('input[type=button]').first().show(); .......but not working

– Joe Doe
Nov 22 '18 at 10:52





Works fine :) But, if I first want to hide then show, it does not work.... First I set: btn.style.visibility = "hidden"; then I tried $(this).find('button').first().show(); and $(this).find('input[type=button]').first().show(); .......but not working

– Joe Doe
Nov 22 '18 at 10:52













@JoeDoe I edited my answer to accommodate your comment

– Ahmad
Nov 22 '18 at 10:59





@JoeDoe I edited my answer to accommodate your comment

– Ahmad
Nov 22 '18 at 10:59













your modification works, but messes other elements in site. Can't I use my version of the button, and use $(this).find to find button by id ? If I add btn.id = r[0].toString()+"btn" then each button has unique id different from tablerow id... ??

– Joe Doe
Nov 22 '18 at 11:11







your modification works, but messes other elements in site. Can't I use my version of the button, and use $(this).find to find button by id ? If I add btn.id = r[0].toString()+"btn" then each button has unique id different from tablerow id... ??

– Joe Doe
Nov 22 '18 at 11:11















Something like: $(requestID+"btn").first().show();

– Joe Doe
Nov 22 '18 at 11:14





Something like: $(requestID+"btn").first().show();

– Joe Doe
Nov 22 '18 at 11:14













@JoeDoe I edited my answer again to show how you might show the button again

– Ahmad
Nov 22 '18 at 11:22





@JoeDoe I edited my answer again to show how you might show the button again

– Ahmad
Nov 22 '18 at 11:22


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428932%2fset-visibility-on-off-on-button-in-table%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

TypeError: fit_transform() missing 1 required positional argument: 'X'