I can't set my table row as link to something. I can use only css and html. I tried different things from div in row to something another, but still can't make it works.
You have two ways to do this:
Using javascript:
<tr onclick="document.location = 'links.html';">
Using anchors:
<tr><td>text</td><td>text</td></tr>
I made the second work using:
table tr td a {
display:block;
height:100%;
width:100%;
}
To get rid of the dead space between columns:
table tr td {
padding-left: 0;
padding-right: 0;
}
Here is a simple demo of the second example: DEMO
I made myself a custom jquery function:
Html
<tr data-href="site.com/whatever">
jQuery
$('tr[data-href]').on("click", function() {
document.location = $(this).data('href');
});
Easy and perfect for me. Hopefully it helps you.
(I know OP want CSS and HTML only, but consider jQuery)
Edit
Agreed with Matt Kantor using data attr. Edited answer above
If you're on a browser that supports it you can use CSS to transform the <a> into a table row:
.table-row { display: table-row; }
.table-cell { display: table-cell; }
<div style="display: table;">
<a href="..." class="table-row">
<span class="table-cell">This is a TD... ish...</span>
</a>
</div>
Of course, you're limited to not putting block elements inside the <a>.
You also can't mix this in with a regular <table>
If you have to use a table, you can put a link into each table cell:
<table>
<tbody>
<tr>
<td>John Smith</td>
<td>123 Fake St</td>
<td>90210</td>
</tr>
<tr>
<td>Peter Nguyen</td>
<td>456 Elm Ave</td>
<td>90210</td>
</tr>
</tbody>
</table>
And make the links fill up the entire cells:
table tbody tr td a {
display: block;
width: 100%;
height: 100%;
}
If you are able to use <div>s instead of a table, your HTML can be a lot simpler, and you won't get "gaps" in the links, between the table cells:
<div class="myTable">
<a href="person1.html">
<span>John Smith</span>
<span>123 Fake St</span>
<span>90210</span>
</a>
<a href="person2.html">
<span>Peter Nguyen</span>
<span>456 Elm Ave</span>
<span>90210</span>
</a>
</div>
Here is the CSS that goes with the <div> method:
.myTable {
display: table;
}
.myTable a {
display: table-row;
}
.myTable a span {
display: table-cell;
padding: 2px; /* this line not really needed */
}
The usual way is to assign some JavaScript to the onClick attribute of the TR element.
If you can't use JavaScript, then you must use a trick:
Add the same link to each TD of the same row (the link must be the outermost element in the cell).
Turn links into block elements: a { display: block; width: 100%; height: 100%; }
The latter will force the link to fill the whole cell so clicking anywhere will invoke the link.
Answer from sirwilliam fits me best. I improved the Javascript with support for hotkey Ctrl + LeftClick (opens page in new tab). Event ctrlKey is used by PC's, metaKey by Mac.
Javascript
$('body').on('mousedown', 'tr[url]', function(e){
var click = e.which;
var url = $(this).attr('url');
if(url){
if(click == 2 || (click == 1 && (e.ctrlKey || e.metaKey))){
window.open(url, '_blank');
window.focus();
}
else if(click == 1){
window.location.href = url;
}
return true;
}
});
Example
http://jsfiddle.net/vlastislavnovak/oaqo2kgs/
You can't wrap a <td> element with an <a> tag, but you can accomplish similar functionality by using the onclick event to call a function. An example is found here, something like this function:
<script type="text/javascript">
function DoNav(url)
{
document.location.href = url;
}
</script>
And add it to your table like this:
<tr onclick="DoNav('http://stackoverflow.com/')"><td></td></tr>
I know this question is already answered but I still don't like any solution on this page. For the people who use JQuery I made a final solution which enables you to give the table row almost the same behaviour as the <a> tag.
This is my solution:
jQuery You can add this for example to a standard included javascript file
$('body').on('mousedown', 'tr[url]', function(e){
var click = e.which;
var url = $(this).attr('url');
if(url){
if(click == 1){
window.location.href = url;
}
else if(click == 2){
window.open(url, '_blank');
window.focus();
}
return true;
}
});
HTML Now you can use this on any <tr> element inside your HTML
<tr url="example.com">
<td>value</td>
<td>value</td>
<td>value</td>
<tr>
When i want simulate a <tr> with a link but respecting the html standards, I do this.
HTML:
<table>
<tr class="trLink">
<td>
Something
</td>
</tr>
</table>
CSS:
tr.trLink {
cursor: pointer;
}
tr.trLink:hover {
/*TR-HOVER-STYLES*/
}
tr.trLink a{
display: block;
height: 100%;
width: 100%;
}
tr.trLink:hover a{
/*LINK-HOVER-STYLES*/
}
In this way, when someone go with his mouse on a TR, all the row (and this links) gets the hover style and he can't see that there are multiple links.
Hope can help someone.
Fiddle HERE
This saves you having to duplicate the link in the tr - just fish it out of the first a.
$(".link-first-found").click(function() {
var href;
href = $(this).find("a").attr("href");
if (href !== "") {
return document.location = href;
}
});
//Style
.trlink {
color:blue;
}
.trlink:hover {
color:red;
}
<tr class="trlink" onclick="function to navigate to a page goes here">
<td>linktext</td>
</tr>
Something along these lines perhaps? Though it does use JS, but that's only way to make a row (tr) clickable.
Unless you have a single cell with an anchor tag that fills the entire cell.
And then, you shouldn't be using a table anyhow.
After reading this thread and some others I came up with the following solution in javascript:
function trs_makelinks(trs) {
for (var i = 0; i < trs.length; ++i) {
if (trs[i].getAttribute("href") != undefined) {
var tr = trs[i];
tr.onclick = function () { window.location.href = this.getAttribute("href"); };
tr.onkeydown = function (e) {
var e = e || window.event;
if ((e.keyCode === 13) || (e.keyCode === 32)) {
e.preventDefault ? e.preventDefault() : (e.returnValue = false);
this.click();
}
};
tr.role = "button";
tr.tabIndex = 0;
tr.style.cursor = "pointer";
}
}
}
/* It could be adapted for other tags */
trs_makelinks(document.getElementsByTagName("tr"));
trs_makelinks(document.getElementsByTagName("td"));
trs_makelinks(document.getElementsByTagName("th"));
To use it put the href in tr/td/th that you desire to be clickable like: <tr href="http://stackoverflow.com">.
And make sure the script above is executed after the tr element is created (by its placement or using event handlers).
The downside is it won't totally make the TRs behave as links like with divs with display: table;, and they won't be keyboard-selectable or have status text. Edit: I made keyboard navigation work by setting onkeydown, role and tabIndex, you could remove that part if only mouse is needed. They won't show the URL in statusbar on hovering though.
You can style specifically the link TRs with "tr[href]" CSS selector.
I have another way. Especially if you need to post data using jQuery
$(document).on('click', '#tablename tbody tr', function()
{
var test="something";
$.post("ajax/setvariable.php", {ID: this.id, TEST:test}, function(data){
window.location.href = "http://somepage";
});
});
Set variable sets up variables in SESSIONS which the page you are going to can read and act upon.
I would really like a way of posting straight to a window location, but I do not think it is possible.
Thanks for this. You can change the hover icon by assigning a CSS class to the row like:
<tr class="pointer" onclick="document.location = 'links.html';">
and the CSS looks like:
<style>
.pointer { cursor: pointer; }
</style>
This method is here to give you a choice. Old css trick: filling the parent with position absolute.
<table>
<tr style=position:relative>
<td><a href=# style=position:absolute;inset:0></a>some
<td>cells
<td>in
<td>a row
</table>
inset:0 is a shorthand for top:0;right:0;bottom:0;left:0
we put <a> inside first <td> because this is a good chance to keep validity: only <td> can be a child of <tr>. But you can place it anywhere in the table and it will work.
Can you add an A tag to the row?
<tr><td>
</td></tr>
Is this what you're asking?
Related
Im trying to delete or at least make a cell disappear in a table in HTML.
I tried to put a class on the cell i want to disappear, then, with CSS, i tried to put a "visibility: hidden;" but it doesnt worck for this particular cell.
<td class="removecell"></td>
.removecell {
visibility: hidden;
}
I expect the cell to disappear, though, nothing happend. No error message.
Here is a link to my Codepen : CodePen Link
You can use this:
<td class="removecell"></td>
.removecell {
display: none;
}
add display:none to your td
.removecell{
display:none
}
and visibility: hidden also work maybe something in your css is overriding it. try using !important
.removecell {
visibility: hidden!important;
}
<td id="removecell"></td>
<script>
//this will shortened table width
document.getElementById('removecell').style.display = "none";
document.getElementById('removecell').style.visibility= "hidden";
</script>
if you want to completly remove/delete the cell let me know but it will cost you to decrease width
I'm stuck with a few lines of code, and I'm unable to solve my issue, so I'm asking for your help.
I would like to change the label's color (id selectSubject), based on the status of checkboxes (French and German).
<div id="chooseSubject" style="margin-bottom:24px;">
<label id="selectSubject" class="form-control">Select your subject(s)</label>
<table style="width:auto;text-align:left;margin-bottom:1px;margin-top:13px" >
<tr>
<th style="font:inherit;padding-right:110px;"><input type="checkbox" id="french" style="display:none; position:absolute; left:9999px;"><label for="french"><span></span>French</label></th>
<th style="font:inherit;"><input type="checkbox" id="german" style="display:none; position:absolute; left:9999px;" class="1" value="1""><label for="german"><span></span>German</label></th>
</tr>
</table>
</div>
I had a look into CSS solutions but i don't understand how the operators work, such as <, >, ~, +, so I couldn't solve my problem.
So just to clarify, if none of the checkboxes is checked, the colour of selectSubject isn't changed, but if any of them is checked, it changes to a colour.
Hopefully these make sense, thanks very much in advance.
jQuery version
What you need to do is capture the change event on your checkboxes and toggle a class.
(When they're clicked, add a css class)
You can do this with jquery like so.
$('#french').on('change', function() {
//When fench checkbox is ticked, add this class, or remove it
$('#selectSubject').toggleClass('add-red-color');
});
$('#german').on('change', function() {
//When german checkbox is ticked, add this class, or remove it
$('#selectSubject').toggleClass('add-green-color');
});
Check this working DEMO
My final solution - based on ProEvilz answer.
var french = document.getElementById('french');
var german = document.getElementById('german');
var cbs = document.querySelectorAll('input[type=checkbox]');
for(var i = 0; i < cbs.length; i++) {
cbs[i].addEventListener('change', function() {
if(german.checked || french.checked) {
document.getElementById("selectSubject").style.color = "black";
} else {
document.getElementById("selectSubject").style.color = "#A9A9A9";
}
});
}
Here's what I was able to get working with help from someone on here. However, I'd like to add it to include the -/+ when expanding/collapsing the rows.
Collapsing table with html
Code: http://jsfiddle.net/5BRsy/3/
HTML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table class="table2">
<th></th><th>server 1</th><th>server 2</th>
<tr><td class="btn">used</td><td>1gb</td><td>2gb</td></tr>
<tr><td class="expand1">drive 1</td><td class="expand1">0.5gb</td><td class="expand1">1gb</td></tr>
<tr><td class="expand1">drive 2</td><td class="expand1">0.5gb</td><td class="expand1">1gb</td></tr>
<tr><td class="btn2">available</td><td>1gb</td><td>2gb</td></tr>
<tr><td class="expand2">drive 1</td><td class="expand2">0.5gb</td><td class="expand2">1gb</td></tr>
<tr><td class="expand2">drive 2</td><td class="expand2">0.5gb</td><td class="expand2">1gb</td></tr>
<tr><td>total</td><td>2gb</td><td>4gb</td></tr>
</table>
CSS
.expand1 { display: none;
}
.expand2 { display: none;
}
JS
$(document).ready(function(){
$(".btn").click(function(){
$(".expand1").toggle();
});
$(".btn2").click(function(){
$(".expand2").toggle();
});
})
I updated your fiddle: http://jsfiddle.net/5BRsy/7/
Basically, you are adding some kind of html that you will use to represent your toggle. In your case I used <span>+</span>.
<tr><td class="btn"><span>+</span> used</td><td>1gb</td><td>2gb</td></tr>
Then, when you click the <span> (or the + sign I should say) you toggle the display of your content, as you were already doing, and then change your + to a -. Clicking it again toggles it back to the way things were.
$(document).ready(function(){
$(".btn span").click(function(){
if($(".btn span").html() == "+") {
$(".btn span").html("-");
} else {
$(".btn span").html("+");
}
$(".expand1").toggle();
});
To render the + and - you can try using :before pseudo-element, however it's just a trifle problem, the problem is your code, it's too messy indeed, I modified it much to make it more perfect. Fistly you don't need the classes expand1 and expand2 assigned to each td that way. You can take benefit of jQuery selectors to hide the rows initially and toggle the rows on click. Here is the modified code:
HTML:
<table>
<th></th><th>server 1</th><th>server 2</th>
<tr><td class="btn">used</td><td>1gb</td><td>2gb</td></tr>
<tr><td>drive 1</td><td>0.5gb</td><td>1gb</td></tr>
<tr><td>drive 2</td><td>0.5gb</td><td>1gb</td></tr>
<tr><td class="btn">available</td><td>1gb</td><td>2gb</td></tr>
<tr><td>drive 1</td><td>0.5gb</td><td>1gb</td></tr>
<tr><td>drive 2</td><td>0.5gb</td><td>1gb</td></tr>
<tr><td>total</td><td>2gb</td><td>4gb</td></tr>
</table>
CSS:
.btn:before {
content:'+ ';
vertical-align:middle;
}
.btn.expanded:before {
content:'- ';
vertical-align:top;
}
JS:
//hide the rows initially
$('.btn').closest('tr').nextUntil('tr:has(td.btn)')
.not(':last-child').toggle();
//register click handler on ready
$(document).ready(function(){
$(".btn").click(function(){
$(this).closest('tr').nextUntil('tr:has(td.btn)')
.not(':last-child').toggle();
$(this).toggleClass('expanded');
});
});
Demo.
You can see that the code is much more concise and cleaner. Here is some related references:
.closest()
:has()
.nextUntil()
I tried to display a image when a mouseover on text occurs. It works fine. And the alignment of the image should be shown such that the end of the image should be at the container. It works fine with the code shown below, Only in IE7. In everything, it gets chopped off.. What is wrong here?
<td valign="middle" class="table_td td" style="width: 347px">
<span class="feature_text" style="cursor:pointer"
onmouseover="ShowPicture('Style16',1)"
onmouseout="ShowPicture('Style16',0)" id="a16">
Storefront Window Decal</span>
<div id="Style16" style="position:relative;height:0px;left:50%;bottom:700%;
visibility:hidden; border:solid 0px #CCC; padding:5px">
<img src="images/window-decal-image.gif"></div>
<script language="javascript" type="text/javascript">
function ShowPicture(id,Source)
{
var vis, elem;
if (1 == Source)
{
vis = "visible";
}
else if (0 == Source)
{
vis = "hidden";
}
else
{
throw new RangeError("Unknown Flag");
}
if (elem = document.getElementById(id))
{
elem.style.visibility = vis;
}
else
{
throw new TypeError("Element with id '"+id+"' does not exist.");
}
return vis;
}
</script>
Can someone help me out. Thanks in advance!!
I'd suggest using background-image on your div instead of a separate <img> tag, and background-position:right; to align it as per your requirements.
You may need a few other related bits of CSS to get it perfect (background-repeat maybe?), but I'd suggest that's the way to do it.
I'd also suggest moving all that style code into a separate CSS <style> element to reduce the clutter in your HTML code and make it more readable.
I want to position a textbox and a button next to each other in an 'elastic' or 'liquid' way (what's the correct term?) like so:
(source: ocactus.org)
When placed in a container of arbitrary width (incl. browser window resizing), the button should right align and take up as much width as it requires while the textbox should use the remaining width. Unfortunately the button's width cannot be fixed in CSS as it depends on the caption (different actions, languages etc.).
What is a good solution for the above that works cross browser?
I was able to get this to work within a table (I know, but it works) where it would correctly handle page resizing as well as the value of the button changing:
<table class="elastic">
<tr>
<td><input class="textbox" type="text"></td>
<td class="button"><input type="button" value="Test Button"></td>
</tr>
</table>
There may be a <div> alternative out there for styling.
EDIT: I revamped my example to use the following style sheet:
.elastic { width:100%; }
.elastic .textbox { width: 100%; }
.elastic .button { width: 1%; }
It's hard to give a definitive answer without seeing some code but the following will size the input 50% of the browser width with the button next to it.
input {width:50%}
<input>
<button>save</button>
HTML:
<div class="widebox">
<input type="text" value="" />
<button>Button</button>
</div>
jQuery:
<script type="text/javascript">
$(document).ready(function(){
var w = $(".widebox");
$(".widebox input:text").css({ width: (w.width - (w.children("button:first").width + 20)) });
});
</script>
Note: Be sure to include the jQuery library in your document's <head>. For speed I recommend using the one hosted by Google: http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
Here's a jQuery extension I wrote based on NGJ Graphics' answer. It takes into account multiple buttons such as Ok/Cancel options.
(function($) {
$.fn.extend({
widebox: function() {
var el = $(this);
var width = 0;
el.children("button").each(function() {
width += $(this).outerWidth( true );
});
el.children("input:text").css({ width: (el.width() - (width + 40)) });
}
});
})(jQuery);