I have this code below:
<li>
<a href="<?php echo base_url()?>home/promos/call" class="promo-call active-call"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/text" class="promo-text"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/data" class="promo-data"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/bo" class="promo-bo"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/recent" class="promo-recent"></a>
</li>
What I want to do is when the user click the class promo-text
it will append an active-text
in the class property
E.g:
<a class="promo-text active-text></a>
and will add this css style: background:url(../images/call_icon_0.png) 0 -72px no-repeat!important;
Of course i need remove the active in the other class.
I have this code below:
<li>
<a href="<?php echo base_url()?>home/promos/call" class="promo-call active-call"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/text" class="promo-text"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/data" class="promo-data"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/bo" class="promo-bo"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/recent" class="promo-recent"></a>
</li>
What I want to do is when the user click the class promo-text
it will append an active-text
in the class property
E.g:
<a class="promo-text active-text></a>
and will add this css style: background:url(../images/call_icon_0.png) 0 -72px no-repeat!important;
Of course i need remove the active in the other class.
Share Improve this question edited Mar 20, 2012 at 9:16 Manse 38.1k11 gold badges86 silver badges111 bronze badges asked Mar 20, 2012 at 7:54 Wondering CoderWondering Coder 1,70211 gold badges31 silver badges51 bronze badges 7- Would you mind using Javascript/JQuery? – MD Sayem Ahmed Commented Mar 20, 2012 at 7:56
- You should use Javascript here – Config Commented Mar 20, 2012 at 8:00
- @SayemAhmed yup. Just don't know how to apply it in my case. – Wondering Coder Commented Mar 20, 2012 at 8:21
- 1 @Gordon sorry, edited the tags – Wondering Coder Commented Mar 20, 2012 at 8:22
- @WonderingCoder: See the answer below. – MD Sayem Ahmed Commented Mar 20, 2012 at 8:25
7 Answers
Reset to default 4If you don't mind using Javascript + Jquery + CSS, try the following.
First create a CSS Class like this -
.active-text
{
background: url(../images/call_icon_0.png) 0 -72px no-repeat;
}
Then use the following JQuery code -
$(document).ready(function()
{
$("li a").click(function()
{
$("li a.active-text").removeClass("active-text");
$(this).addClass("active-text");
});
});
jsFiddle demo.
To add any specific CSS style to an element, use the css() function -
$(this).css("color", "red");
This will dynamically add color:red
CSS style to the selected element. You can add any CSS rule in this way.
Using javascript & jquery you could use http://api.jquery./click/:
$('li a').click(function(){
// remove all classes
$('li a').removeClass('active-text');
// add class to the selected element
$(this).addClass('active-text);
});
Instead of using a separate class, you can use the pseudo class a:active
a.promo-text {
/* some style */
}
a.promo-text:active {
background:url(../images/call_icon_0.png) 0 -72px no-repeat!important;
}
if you have multiple backgrounds for each link,
a.promo-text:active {
/* text_icon background */
}
a.promo-call:active {
/* call_icon background */
}
a.promo-recent:active {
/* recent_icon background */
}
and so on
Set it manually since it will redirect to another page after clicking.
For example in call page, this will be the code:
<li>
<a href="<?php echo base_url()?>home/promos/call" class="promo-call active-text active-call"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/text" class="promo-text"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/data" class="promo-data"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/bo" class="promo-bo"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/recent" class="promo-recent"></a>
</li>
In text page:
<li>
<a href="<?php echo base_url()?>home/promos/call" class="promo-call active-call"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/text" class="promo-text active-text"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/data" class="promo-data"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/bo" class="promo-bo"></a>
</li>
<li>
<a href="<?php echo base_url()?>home/promos/recent" class="promo-recent"></a>
</li>
Using pure CSS you won't be able to set a new class upon a click, you would need Javascript for that. (see xylar's response for an example)
If you are interested in changing the style upon hover, i.e. when the user moves his mouse over a list item, you could use the li:hover
CSS directive.
$(document).ready(function(){
$("li a").click(function(){
$(this).addClass("class name here").parent().siblings().children("a").removeClass("to remove class name here");
});
});
Should get you there.
If I'm right, then you can't add inline style with !important
option, therefore you should create separate class or id for, that case ( class would be better I supose ). Also. You may have more than one list on the page, therefore it is better to work with one specific, so you take closest to your li, but if your link will go somewhere ( not on page action ), then it will reload and there is no point in JavaScript. Better to add with PHP
then. Or if you parse it with Javascript somehow or do Ajax call, then it is fine (probably).
<style type="text/css">
.active-text {
background: url(../images/call_icon_0.png) 0 -72px no-repeat !important;
}
</style>
<script type="text/javascript">
$(function()
{
$('li a').click(function(){
var $this = $(this);
$this.closest('ul').find( 'li a' ).removeClass('active-text');
$this.addClass('active-text);
});
});
</script>