waleedd32
waleedd32

Reputation: 533

id not working using jquery

Hi everyone i have 3 buttons one is working others not could someone tell me reasons.. in jquery i have commanded it to hide id's which has # in it but middle button which is working and it has 2 id's one with # and one without i take the one without # away it doesnt work.. i am new in programming. sorry my english

< style>

button.rond { width:50px; height:50px; }

< /style> < /head>

< body> < table id="t01" style="width:750px">

< tr id="#panel6">

< td>Coo< /td>  

< td>Fin< /td> < td> < button class="rond" id="#panel6">Stop< /button>< /td>

< /tr>

< tr id="panel7">

< td>Coo< /td>  

< td>Euro< /td>< td>< button class="rond" id="#panel7">Stop< /button>< /td>

< /tr>

< tr id="#panel8">

< td>Coo< /td>  

< td>Fin< /td> < td><button class="rond">Stop< /button>< /td>

< /tr> < /table> < script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">< /script>

< script>

$(function(){

$('.rond').on('click',function(){

var id =$(this).attr('id');

$(id).hide(1);

     });   
});

< /script>

< /table>

< /body>

Upvotes: 0

Views: 493

Answers (2)

Akshay
Akshay

Reputation: 14348

jQuery returns only the id of the element it won't provide a # include it like this

var id =$(this).attr('id');
$('#'+id).hide(1);`

Or you can simply use $(this) which is a lot easier as @Pranav C Balan suggests

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115212

You can get id by this.id, also you need to add # before id selector. But here there is no need to get id, you can just use $(this) instead. Here this refers to the clicked dom element.

$(function(){    
   $('.rond').on('click',function(){
       $(this).hide();
   });   
});

Upvotes: 1

Related Questions