1

I have an li as below

<li class="active">
        <a href="#" onclick="editCategory(this)" class="active" value="14-client" id=""> qqqq
        <i class="glyphicon glyphicon-trash pull-right" style="display:none"></i></a>
    </li>

It has an i tag which is hidden at beginning <i class="glyphicon glyphicon-trash pull-right" style="display:none"></i>

I want to show this i tag only when mouse is clicked on the anchor tag so I wrote onclick="editCategory(this)"

and this is my function

function editCategory(id)
    {
        alert('category');
        $(this).children().show();
    }

so when I click on the anchor tag then alert works alert('category') but the i tag is not shown.

Can anybody please point out the mistake I have done

SpringLearner
  • 13,738
  • 20
  • 78
  • 116

5 Answers5

1

Use:


function editCategory(id)
{
    alert('category');
    $(id).find('i').show("100");
}

DEMO


Also You're Missing $(document).ready(function(){...}); in your javascript if you're using JQuery be sure that it becomes ready as soon as your DOM becomes ready.
So I've changed your code as:
$(document).ready(function(){
 // Here $(this) will be `document` object
   $(".active").click(function(){ 
      //alert('category');
      $(this).find('i').show("100");  // Now this refers to object from which event is bubbled or generated.
    }); 
});

and your html will be:

<a href="#" class="active" value="14-client" id="qqq"> qqqq
        <i class="glyphicon glyphicon-trash pull-right" style="display:none">aaa</i></a>

DEMO

I mean this is era of JQuery Man. and you're still using inline-javascript. just get rid of it.


Also In this code:
function editCategory(id)
{
    alert('category');
    console.log($(this)); //or try alert instead of console.log, it Will Log/alert Window Object 
    console.log($(id)); // This will log <a> Object.
    $(id).find('i').show("100");
}

you're passing id as object this(anchor obj) to function, and $(this)(the object you're acessing) is now refering (in your case) to window object.

DEMO

Hope it helps.

Vedant Terkar
  • 4,553
  • 8
  • 36
  • 62
1

Try something like this instead (Put this between <head><script>...</script></head>):

$(function(){
    $('a.active').on('click', function(e){
        e.preventDefault();
        $(this).find('i').show();
    });
});

Use this HTML:

<li class="active">
    <a href="#" class="active" value="14-client" id=""> qqqq
        <i class="glyphicon glyphicon-trash pull-right" style="display:none"></i>
    </a>
</li>
The Alpha
  • 143,660
  • 29
  • 287
  • 307
0

It is an issue of parameter and not other thing : $(id) and not $(this)

function editCategory(id)
    {
        alert('category');
        $(id).children().show();
    }

$(id) and not $(this)

Because $(this).children().show(); means the this of function and not this of a Tag .

DEMO: http://jsfiddle.net/abdennour/bUtA3/

Community
  • 1
  • 1
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
0

Demo try this

$("li.active a.active").click(function() {
        $(this).find("i").show();

});
Balachandran
  • 9,567
  • 1
  • 16
  • 26
-1

Use this. <i> is the child of your <a> tag. Demo Fiddle

You are already passing this as a parameter to function editCategory(id) referenced by id.

function editCategory(id)
{
    alert('category');
    $(id).children('i').show();
}

Or

function editCategory(id)
{
    alert('category');
    $(id).find('i').show();
}

Other alternative,

$('i',id).show();
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
  • Please Recheck, in my code only one `` is shown - which we click. You can check in console. – Shaunak D Jun 03 '14 at 04:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54958/discussion-between-shaunakde-and-jquerylearner). – Shaunak D Jun 03 '14 at 04:45
  • I have a doubt,can you help me? – SpringLearner Jun 05 '14 at 09:44