1

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!

3 Answers3

0

Updated your code, removed the using of the method val that uses only for forms, removed showing dropdown by adding hash in the link, it was replaced just adding class active that makes dropdown visible. PS Sorry for my bad English...

$(document).ready(function(){
  $(".parent").click(function(e) {
    e.preventDefault();
    const $link = $(this);
    const data="<p>A</p><p>B</p>"
    const inputVal = $link.data('selected', '');
    const resultDropdown = $link.siblings(".child");
    
    resultDropdown.html(data).addClass('active');
  });
  $(document).on("click", ".child p", function(){
   const $selected = $(this);
    const $link = $selected.parents('td').find('.parent');
    
    $link.text($selected.text());
    $link.data('selected', $selected.text());
    $selected.parent(".child").removeClass('active');
    
  });
});
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.active{
  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%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<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>
Ivan Karaman
  • 1,224
  • 1
  • 7
  • 11
-1

Please add This new 2 css

   table.coolTable td {
        position: relative;  
    }
    .child-dropdown { 
        top: 0px;
        left: 29px;
    }
Piyush Marvaniya
  • 2,496
  • 2
  • 16
  • 28
-1

By Using Anchor Tag(): Use "javascript:void(0)" and id to write click function in JavaScript like following:

<a href="javascript:void(0)" id="toCallFunction">login</a>

$('#toCallFunction').click(function(){
    doSomething();
    alert('something');
}
Ajoshi
  • 153
  • 1
  • 4
  • 22