I'm trying to make a clickable <td>
, end up with below code :
HTML :
<table class="coolTable">
<tr>
<td>
<a id='parent1' href='#child1' class="parent">G</a>
<div id="child1" class="child child-dropdown"></div>
</td>
</tr>
<tr>
<td>
<a id='parent2' href='#child2' class="parent">C</a>
<div id="child2" class="child child-dropdown"></div>
</td>
</tr>
</table>
JQUERY :
$(document).ready(function(){
$(".parent").click(function() {
$(this).parent(".child").empty();
var data="<p>A</p><p>B</p>"
/* Get input value on change */
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".child");
// Display the returned data in browser
resultDropdown.html(data);
});
$(document).on("click", ".child p", function(){
$(this).parents(".parent").find('a').val("A");
$(this).parent(".child").empty();
});
});
CSS :
a {
color:white;
text-decoration:none;
}
.child p {
margin: 0;
padding: 5px 8px;
border: 1px solid #CCCCCC;
cursor: pointer;
background : white;
}
.child p:hover {
background: #f2f2f2;
}
.child-dropdown {
position: absolute;
display: none;
z-index: 9;
color: red;
}
.child-dropdown:target {
display: block;
}
table.coolTable td {
background-color: green;
color: green;
margin: 0;
padding: 5px 8px;
}
table.coolTable td:hover {
background-color: green;
color: green;
}
td a {
display: block;
width: 100%;
}
https://jsfiddle.net/kakatua/cnej7dfs/
If i clicked a <td>
it will execute the rest of the code and show me an option for me to replace the innerHTML
of an <a>
.
but when i implemented this on the actual web page, when i clicked the <td>
it will jump to the bottom of the page (and will show my dropdown list).
i dont want the page to jump after clicked the <td>
i've used preventDefault, a="#/", and yes it will not jump, but also, its not returning my dropdown list.
is there any way to prevent this jumping thing but still execute my dropdown list?
or anyone can suggest me to make clickable <td>
without using <a>
Thanks!