We help IT Professionals succeed at work.

How to focus on the clicked <a> tag ?

cofactor
cofactor used Ask the Experts™
on
Here is my JQuery code

<script type="text/javascript" src="jquery-1.4.2.min.js"> </script>
<script type="text/javascript">
            $(document).ready(function()
            {
                  $("a").bind("click", function(){
                  


                          var param=$(this).attr("id");
                    
                        $("."+param).each(function()
                        {
                              this.click();
                              
                        });
                        }
                  );                  
            });
            
</script>


Now, I wish  to keep the focus on the clicked  <a>  tag . What to modify in this code ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
added focus();      in the code.

Still Not working .

Here is the modified code:


<script type="text/javascript" src="jquery-1.4.2.min.js"> </script>
<script type="text/javascript">
            $(document).ready(function()
            {
                  $("a").bind("click", function(){
                  


                    var param=$(this).attr("id");
                    
                      $("#"+param).focus();

                        $("."+param).each(function()
                        {
                              this.click();
                              
                        });
                        }
                  );                  
            });
            
</script>
leakim971Multitechnician
Top Expert 2014

Commented:
could you confirm the object with the class like ("."+param). are not anchor ?
Else seems you call infinitely  $("a").bind("click again and again

Author

Commented:
>>>could you confirm the object with the class like ("."+param). are not anchor ?

yes. they are not anchors . they are checkboxes.

>>>Else seems you call infinitely  $("a").bind("click again and again.

not infinitely . I  have finite checkboxes with the same class values .

For your information ,  the id of the anchor tag  is same as the class value of the finite number of checkboxes.

So, when I click the anchor, all those checkboxes whose class value is same as the id value of the anchor are also clicked.

This is a working code.

Now,I want to add the focus on the clicked anchor. how to do that ?

Author

Commented:
comments please.
leakim971Multitechnician
Top Expert 2014
Commented:
Check this simple example, you will understand what happen :


<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"> </script>
<script type="text/javascript">
	$(document).ready(function() {
		$("a").bind("click", function(){
			alert("I'm calling myself");
			var param = $(this).attr("id");
			$("#"+param).focus();
			$("."+param).each(function() {
				$(this).click();
			});
		});                  
	});
</script>
</head>
<body>
<a href="http://www.google.com" class="myself" id="myself">Click on me to fire me again and again!</a>
</body>
</html>

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
Seems you want soimething like this :


<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"> </script>
<script type="text/javascript">
	$(document).ready(function() {
		$("a").bind("click", function(e){
			e.preventDefault(); // remove this line it if your want to go Google at the end
			alert("fire myself");
			var CheckboxToClick = $(this).attr("value");
			$(this).focus();
			$("."+CheckboxToClick).each(function() {
				$(this).attr("checked", true);
			});
		});                  
	});
</script>
</head>
<body>
<a href="http://www.google.com" class="myself" id="myself" value="CheckboxToClick" >Click on me to check the boxes!</a>
<input id="cb0" type="checkbox" class="CheckboxToClick" />
<input id="cb1" type="checkbox" class="DontCheckMe" />
<input id="cb2" type="checkbox" class="CheckboxToClick" />
</body>
</html>

Open in new window