how to change font awesome icons on click? - html

I'm use Font awesome icons in my vBulletin style and i want to change the (minus) icon to (plus) when click ! Is there a way to do it ?
<a rel="nofollow" style="float:left" href="#" onclick="return
toggle_collapse('forumbit_$forumid')">
<span style="color:#24356C;" >
<i class="fa fa-minus-square-o fa-2x"></i>
</span>
</a>

I think this question(and the answer) can help you do what you want :)
Change an element's class with JavaScript

Assign an id to your icon, e.g.
<i class="fa fa-minus-square-o fa-2x" id="i-1"></i>
The id can be dynamic value like 'i-$forumid' depend on how you generate your html.
In your toggle_collapse('forumbit_$forumid') function,
Add
var e = document.getElementById('i-1');
if (e.classList.contains('fa-plus-square-o')) {
e.classList.toggle('fa-plus-square-o');
} else {
e.classList.add('fa-plus-square-o');
}
or
function toggle_collapse(forumID) {
...
var iconID = 'i-' + formumID.replace('forumbit_','')
var e = document.getElementById(iconID);
if (e.classList.contains('fa-plus-square-o')) {
e.classList.toggle('fa-plus-square-o');
} else {
e.classList.add('fa-plus-square-o');
}
...
}

Related

How to change title attribute inside a tag when clicked with Angular?

I'm trying to change the title attribute inside an tag when clicked. If you click it again the title attribute should change it back to its initial state and so it goes. I can make the change of the title happen, but I'm having trouble making it go back to its initial state when clicked again. Here's the code:
HTML
<a title="1" id="initial" role="button" (click)="magic()">
<p>test</p>
</a>
TS
magic(): void {
document.getElementById("initial")?.setAttribute("title", "2");
}
To do that you need to store somewhere a default value of the title and then toggle it on click.
For example you can have defaultTitle property in your class, and then when you need to switch back to a default value you can just reuse that property:
TS
private defaultTitle = 'default-value';
title = this.defaultTitle;
magic(): void {
if (this.title === this.defaultTitle) {
this.title = 'new-value';
} else {
this.title = this.defaultTitle;
}
}
HTML
<a [title]="title" id="initial" role="button" (click)="magic()">
<p>test</p>
</a>
You can use angular binding with a variable.
HTML
<a [title]="title" id="initial" role="button" (click)="magic()">
<p>test</p>
</a>
or
<a title="{{title}}" id="initial" role="button" (click)="magic()">
<p>test</p>
</a>
TS
title = 1;
magic(): void {
this.title = 1 + this.title % 2;
}

Asp Mvc views with jQuery

I have two views and each view contains an link with data-action attribute to call jQuery function called toggle menu like this:
<div id="hide-menu" class="btn-header pull-right">
<span>
<a href="javascript:void(0);" class="toggleMenu" data-action="toggleMenu" title="Collapse Menu")">
<i class="fa fa-reorder"></i>
</a>
</span>
</div>
jQuery:
toggleMenu: function () {
if (!$.root_.hasClass("menu-on-top")) {
$('html').toggleClass("hidden-menu-mobile-lock");
$.root_.toggleClass("hidden-menu");
$.root_.removeClass("minified");
} else if ($.root_.hasClass("menu-on-top") && $.root_.hasClass("mobile-view-activated")) {
$('html').toggleClass("hidden-menu-mobile-lock");
$.root_.toggleClass("hidden-menu");
$.root_.removeClass("minified");
}
}
one of the views calls the function properly but the another doesn't, What is the possible reason for that??
in fact I haven't known the actual reason of the issue, maybe as Ramesh said in the comments was the problem.
I solved it by putting the function in the same document (not in another js file) and in the element, I have used onClick attribute to execute the function.
<div id="hide-menu" class="btn-header pull-right">
<span>
<a href="javascript:void(0);" class="toggleMenu" onclick="toggle()" title="Collapse Menu">
<i class="fa fa-reorder"></i>
</a>
</span>
</div>
<script>
function toggle() {
if (!$.root_.hasClass("menu-on-top")) {
$('html').toggleClass("hidden-menu-mobile-lock");
$.root_.toggleClass("hidden-menu");
$.root_.removeClass("minified");
} else if ($.root_.hasClass("menu-on-top") && $.root_.hasClass("mobile-view-activated")) {
$('html').toggleClass("hidden-menu-mobile-lock");
$.root_.toggleClass("hidden-menu");
$.root_.removeClass("minified");
}
}
</script>

Conditionally displaying template content inside ng-repeat

I'm using ng-repeat to loop render an angular template potentially thousands of times, and wondering what is the most efficient way to go about conditionally altering it's display. For example, each item in the loop has a 'type', which has a corresponding icon. There are a few ways to go about this:
Purely css/html - this seems the most efficient angular-wise, but will mean a lot more html in the DOM
<li class="{{item.type}}">
<i class="type-1-icon"></i>
<i class="type-2-icon"></i>
<i class="type-3-icon"></i>
</li>
And then in the css:
li i { display:none; }
li.type-1 i.type-1-icon { display:block; }
li.type-2 i.type-2-icon { display:block; }
li.type-3 i.type-3-icon { display:block; }
Using ng-if - tidies up the dom, but could be less efficient evaluating the ng-if for lots of records?
<li>
<i class="type-1-icon" ng-if="item.type == 'type-1'"></i>
<i class="type-2-icon" ng-if="item.type == 'type-2'"></i>
<i class="type-3-icon" ng-if="item.type == 'type-3'"></i>
</li>
Using ng-switch
<li ng-switch on="item.type">
<i class="type-1-icon" ng-switch-when="type-1"></i>
<i class="type-2-icon" ng-switch-when="type-2"></i>
<i class="type-3-icon" ng-switch-when="type-3"></i>
</li>
Using ng-show (same for ng-hide). Similar to #1 I'm guessing
<li>
<i class="type-1-icon" ng-show="item.type == 'type-1'"></i>
<i class="type-2-icon" ng-show="item.type == 'type-2'"></i>
<i class="type-3-icon" ng-show="item.type == 'type-3'"></i>
</li>
Anyway, I realise that all 4 approaches will work fine, but I'd love to learn a bit more about which is best for a potentially large dataset and why. Thanks!

Trying to put a button into a link

I have a clickable span, which has a button in it. I want the span to go to one href and the button to another. Here is the code I currently have:
<script>
function deleteAlbum(url) {
var txt;
var r = confirm("Do you really want to delete this album?");
if (r == true) {
window.location.href = url;
}
document.getElementById("demo").innerHTML = txt;
}
</script>
<a href='gallery.php?album=16022015NewTest'>
<span class='album'>
<p>New Test</p>
<p>16/02/2015</p>
<button class='delete' onClick=deleteAlbum('gallery.php?delete_album=16022015NewTest')>
Delete Album
</button>
</span>
</a>
Anyone have any ideas?
You want to read up on event bubbling - the quirksmode explanation in that answer is very nice.
Here's one possible fix for your example:
<script>
function gotoAlbum(albumId) {
window.location.href="gallery.php?album="+albumId;
}
function deleteAlbum(e, url) {
var r = confirm("Do you really want to delete this album?");
if (r == true) {
window.location.href = url;
}
e.stopPropagation();
}
</script>
<span class='album' onClick="gotoAlbum('16022015NewTest');" >
<p>New Test</p>
<p>16/02/2015</p>
<button class='delete' onClick="deleteAlbum(event, 'gallery.php?delete_album=16022015NewTest');">Delete Album</button>
</span>
Aside: as the other comments mention, a button inside an anchor is odd - it's not compliant HTML5 - see this question
You don't. Don't put <button> inside <a>.
According to atmd's comment restructure your html: Don't put button inside a. To be html conform use quotes for attribute values:
<div class="album">
<a href="gallery.php?album=16022015NewTest">
<p>New Test</p>
<p>16/02/2015</p>
</a>
<button class="delete" onClick="deleteAlbum('gallery.php?delete_album=16022015NewTest')">Delete Album</button>
</div>
So the a tag does not include the button tag. It is also a good idea not to have block elements inside inline elements. The span tag has already been changed to a div tag. You might also change the p tags to span. Then the html looks like this:
<div class="album">
<a href="gallery.php?album=16022015NewTest">
<span>New Test</span>
<span>16/02/2015</span>
</a>
<button class="delete" onClick="deleteAlbum('gallery.php?delete_album=16022015NewTest')">Delete Album</button>
</div>

Duplicate id issue, cannot reuse an id

I'm working on the following script :
http://www.andwecode.com/playground-demo/pop-up-login-signup-box-jquery/#
I've modified the Gmail and Facebook boxes in it, and I would like them to present a login form when I click on them. I tried this with Gmail box :
<a href="#1" class="social_box google" id="login_form" >
<span class="icon"><i class="fa fa-google-plus"></i></span>
Basically, I added the ID : (id="login_form") and it worked fine !
The problem is that you can use the ID just once. and when you do that the login button became useless ..
any idea how to make all the boxes go for the login form ?
The reason the functionality works only for one scenario is that 'ID's have to be unique', if you have duplicate ID's it will recognize only the id for the first element and it will ignore the rest.
You can achieve your result in two ways
1) have same class for all the boxes fb box,google box etc.
Example: box-form class for all the boxes
HTML:
<a href="#1" class="social_box google box-form" >
<span class="icon"><i class="fa fa-google-plus"></i></span>
<a href="#1" class="social_box fb box-form" >
<span class="icon"><i class="fa fa-google-plus"></i></span>
Jquery:
$('.box-form').click(function() {
//code
});
2) Apply different id for each box and combine the id's in jquery
HTML:
<a href="#1" class="social_box google" id="login-form" >
<span class="icon"><i class="fa fa-google-plus"></i></span>
<a href="#1" class="social_box fb" id="fb-form">
<span class="icon"><i class="fa fa-google-plus"></i></span>
Jquery:
$('#login-form, #fb-form').click(function() {
//code
});