MRizq
MRizq

Reputation: 271

How Jquery/Javascript dynamically add td (table)

I need help, any ideas?

Firstly i want to hide some input element when page open, My approach creating dropdown $counter (1-8) and then depending on $counter Example:3.

I create/show existing element dynamically ($counter)<td></td> inside tr #ID.

How jQuery solve this problem?

Can u provide me example of switch case in jQuery to show/hide with defined td #ID?

Thanks Before

Rizq

Upvotes: 0

Views: 1085

Answers (2)

RobG
RobG

Reputation: 147353

Suppose your input has id of "input-1", then you can hide it simply doing:

// Assign a value to counter, might be some function to do that
var counter = '1';

// Get a reference to the element and hide it
var input = document.getElementById('input-' + counter);
input.style.display = 'none';

// or 

input.style.visibility = 'hidden';

and to show it again:

input.style.display = ''; // empty string

// or 

input.style.visibility = 'visible';

The difference between the display and visibility properties is that the former will remove the element completely from the document flow, whereas the second will just make it invisible (read the relevant parts of the W3C CSS spec). Messing with display might make the document jump about a bit as it reflows, messing with visibility keeps it stable but leaves a blank space. Your choice which suits best.

You can hide and show any kind of element using the same method. If you want the parent cell or row, go up the parentNode chain till you hit the first TD or TR as appropriate and hide it.

There is a bit of a tutorial on the Mozilla developer site for messing with table elements using the DOM APIs.

Upvotes: 0

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18344

To hide a cell row, you can do this:

$("#your_td").closest("tr").hide();

That's becasuse it's more logical to hide all the row than a single cell.

Hope to have understood you.

Upvotes: 1

Related Questions