How to give condition in image tag using angular 6?










1















My webpage contains,



 <ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"
(click)="changeImage(id);">
<img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
horizontal.items[i].title
</button>
</ng-container>


My ts file:



 changeImage(option)
if(option == "Trans")


this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);

else if(option == "Col")

this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);

else if(option == "Business")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);





So in my css I have kept the images which needs to be changed onclick of the button.



.imgChange
background-image: url('../assets/Trans_2.png');
color:blue;

.imgChange1
background-image: url('../assets/Trans_3.png');
color:blue;

.imgChange2
background-image: url('../assets/Trans_4.png');
color:blue;



How can I give condition in img tag, so that onclick of a button to change the image?










share|improve this question
























  • First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?

    – Sunil Singh
    Nov 11 '18 at 18:26











  • @Sai, accept answer if you found one.

    – Mahi
    Nov 12 '18 at 17:54















1















My webpage contains,



 <ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"
(click)="changeImage(id);">
<img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
horizontal.items[i].title
</button>
</ng-container>


My ts file:



 changeImage(option)
if(option == "Trans")


this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);

else if(option == "Col")

this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);

else if(option == "Business")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);





So in my css I have kept the images which needs to be changed onclick of the button.



.imgChange
background-image: url('../assets/Trans_2.png');
color:blue;

.imgChange1
background-image: url('../assets/Trans_3.png');
color:blue;

.imgChange2
background-image: url('../assets/Trans_4.png');
color:blue;



How can I give condition in img tag, so that onclick of a button to change the image?










share|improve this question
























  • First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?

    – Sunil Singh
    Nov 11 '18 at 18:26











  • @Sai, accept answer if you found one.

    – Mahi
    Nov 12 '18 at 17:54













1












1








1








My webpage contains,



 <ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"
(click)="changeImage(id);">
<img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
horizontal.items[i].title
</button>
</ng-container>


My ts file:



 changeImage(option)
if(option == "Trans")


this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);

else if(option == "Col")

this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);

else if(option == "Business")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);





So in my css I have kept the images which needs to be changed onclick of the button.



.imgChange
background-image: url('../assets/Trans_2.png');
color:blue;

.imgChange1
background-image: url('../assets/Trans_3.png');
color:blue;

.imgChange2
background-image: url('../assets/Trans_4.png');
color:blue;



How can I give condition in img tag, so that onclick of a button to change the image?










share|improve this question
















My webpage contains,



 <ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"
(click)="changeImage(id);">
<img [src]= horizontal.items[i].icon alt="image not found" class="icon"/>
horizontal.items[i].title
</button>
</ng-container>


My ts file:



 changeImage(option)
if(option == "Trans")


this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);

else if(option == "Col")

this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);

else if(option == "Business")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);





So in my css I have kept the images which needs to be changed onclick of the button.



.imgChange
background-image: url('../assets/Trans_2.png');
color:blue;

.imgChange1
background-image: url('../assets/Trans_3.png');
color:blue;

.imgChange2
background-image: url('../assets/Trans_4.png');
color:blue;



How can I give condition in img tag, so that onclick of a button to change the image?







html css angular angular6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '18 at 16:51







sai

















asked Nov 11 '18 at 15:57









saisai

1613




1613












  • First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?

    – Sunil Singh
    Nov 11 '18 at 18:26











  • @Sai, accept answer if you found one.

    – Mahi
    Nov 12 '18 at 17:54

















  • First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?

    – Sunil Singh
    Nov 11 '18 at 18:26











  • @Sai, accept answer if you found one.

    – Mahi
    Nov 12 '18 at 17:54
















First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?

– Sunil Singh
Nov 11 '18 at 18:26





First of all, make your decision how you want to display the image. Either by img src or setting the background image by css ?

– Sunil Singh
Nov 11 '18 at 18:26













@Sai, accept answer if you found one.

– Mahi
Nov 12 '18 at 17:54





@Sai, accept answer if you found one.

– Mahi
Nov 12 '18 at 17:54












2 Answers
2






active

oldest

votes


















2














You should add image in a div as below:-



ts



 imgDefault = 'imgChange'; <-- default;

changeImage(option)
if(option == "Trans")


this.imgDefault = 'imgChange';
console.log("this.imgDefault :",this.imgDefault);

else if(option == "Col")

this.imgDefault = 'imgChange1';
console.log("this.imgDefault :",this.imgDefault1);

else if(option == "Business Process Re-engineering")
this.imgDefault = 'imgChange2';
console.log("this.imgDefault :",this.imgDefault2);





.html



 <ng-container *ngFor="let horizontal of categories">
<ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
<button [ngClass]="'selected-color' : i==selectedIndex "
[ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"click)="changeImage(id);">

horizontal.items[i].title
</button>
</ng-container>
</ng-container>

<div [ngClass]="imgDefault"></div>


Link:- https://stackblitz.com/edit/angular6-sqemmd?file=app/app.component.html






share|improve this answer

























  • Instead of <ng-container>. You should use <div>

    – Mahi
    Nov 11 '18 at 16:13











  • Thanks for your reply. I tried the same. But Im not able to get the image.

    – sai
    Nov 11 '18 at 16:46











  • Is this.imgDefault is getting changed onclick?

    – Mahi
    Nov 11 '18 at 16:51











  • Yes, It needs to be changed on onclick of the button

    – sai
    Nov 11 '18 at 17:01











  • Inspect the div and check whether image is getting loaded on network tab

    – Mahi
    Nov 11 '18 at 17:03


















0














You can directly add class names based on the conditions - you are trying to add images from css replace the img tag as div which will render background image based on the class applied



Make sure if you want your images in the loop, if not move your div outside *ngFor



<div [class.imgChange]="imgDefault === 'imgChange'" 
[class.imgChange1]="imgDefault === 'imgChange1'"
[class.imgChange2]="imgDefault === 'imgChange2'" ></div>


This will add all your classes based on the condition and the div will render the img - Hope this works Happy coding !!






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250498%2fhow-to-give-condition-in-image-tag-using-angular-6%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You should add image in a div as below:-



    ts



     imgDefault = 'imgChange'; <-- default;

    changeImage(option)
    if(option == "Trans")


    this.imgDefault = 'imgChange';
    console.log("this.imgDefault :",this.imgDefault);

    else if(option == "Col")

    this.imgDefault = 'imgChange1';
    console.log("this.imgDefault :",this.imgDefault1);

    else if(option == "Business Process Re-engineering")
    this.imgDefault = 'imgChange2';
    console.log("this.imgDefault :",this.imgDefault2);





    .html



     <ng-container *ngFor="let horizontal of categories">
    <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
    <button [ngClass]="'selected-color' : i==selectedIndex "
    [ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"click)="changeImage(id);">

    horizontal.items[i].title
    </button>
    </ng-container>
    </ng-container>

    <div [ngClass]="imgDefault"></div>


    Link:- https://stackblitz.com/edit/angular6-sqemmd?file=app/app.component.html






    share|improve this answer

























    • Instead of <ng-container>. You should use <div>

      – Mahi
      Nov 11 '18 at 16:13











    • Thanks for your reply. I tried the same. But Im not able to get the image.

      – sai
      Nov 11 '18 at 16:46











    • Is this.imgDefault is getting changed onclick?

      – Mahi
      Nov 11 '18 at 16:51











    • Yes, It needs to be changed on onclick of the button

      – sai
      Nov 11 '18 at 17:01











    • Inspect the div and check whether image is getting loaded on network tab

      – Mahi
      Nov 11 '18 at 17:03















    2














    You should add image in a div as below:-



    ts



     imgDefault = 'imgChange'; <-- default;

    changeImage(option)
    if(option == "Trans")


    this.imgDefault = 'imgChange';
    console.log("this.imgDefault :",this.imgDefault);

    else if(option == "Col")

    this.imgDefault = 'imgChange1';
    console.log("this.imgDefault :",this.imgDefault1);

    else if(option == "Business Process Re-engineering")
    this.imgDefault = 'imgChange2';
    console.log("this.imgDefault :",this.imgDefault2);





    .html



     <ng-container *ngFor="let horizontal of categories">
    <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
    <button [ngClass]="'selected-color' : i==selectedIndex "
    [ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"click)="changeImage(id);">

    horizontal.items[i].title
    </button>
    </ng-container>
    </ng-container>

    <div [ngClass]="imgDefault"></div>


    Link:- https://stackblitz.com/edit/angular6-sqemmd?file=app/app.component.html






    share|improve this answer

























    • Instead of <ng-container>. You should use <div>

      – Mahi
      Nov 11 '18 at 16:13











    • Thanks for your reply. I tried the same. But Im not able to get the image.

      – sai
      Nov 11 '18 at 16:46











    • Is this.imgDefault is getting changed onclick?

      – Mahi
      Nov 11 '18 at 16:51











    • Yes, It needs to be changed on onclick of the button

      – sai
      Nov 11 '18 at 17:01











    • Inspect the div and check whether image is getting loaded on network tab

      – Mahi
      Nov 11 '18 at 17:03













    2












    2








    2







    You should add image in a div as below:-



    ts



     imgDefault = 'imgChange'; <-- default;

    changeImage(option)
    if(option == "Trans")


    this.imgDefault = 'imgChange';
    console.log("this.imgDefault :",this.imgDefault);

    else if(option == "Col")

    this.imgDefault = 'imgChange1';
    console.log("this.imgDefault :",this.imgDefault1);

    else if(option == "Business Process Re-engineering")
    this.imgDefault = 'imgChange2';
    console.log("this.imgDefault :",this.imgDefault2);





    .html



     <ng-container *ngFor="let horizontal of categories">
    <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
    <button [ngClass]="'selected-color' : i==selectedIndex "
    [ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"click)="changeImage(id);">

    horizontal.items[i].title
    </button>
    </ng-container>
    </ng-container>

    <div [ngClass]="imgDefault"></div>


    Link:- https://stackblitz.com/edit/angular6-sqemmd?file=app/app.component.html






    share|improve this answer















    You should add image in a div as below:-



    ts



     imgDefault = 'imgChange'; <-- default;

    changeImage(option)
    if(option == "Trans")


    this.imgDefault = 'imgChange';
    console.log("this.imgDefault :",this.imgDefault);

    else if(option == "Col")

    this.imgDefault = 'imgChange1';
    console.log("this.imgDefault :",this.imgDefault1);

    else if(option == "Business Process Re-engineering")
    this.imgDefault = 'imgChange2';
    console.log("this.imgDefault :",this.imgDefault2);





    .html



     <ng-container *ngFor="let horizontal of categories">
    <ng-container *ngFor="let horizontalval of horizontal.items;let i=index">
    <button [ngClass]="'selected-color' : i==selectedIndex "
    [ngStyle]="'padding-top':horizontal.items[i].title==='Business'?'2.56%':(horizontal.items[i].title==='Plat' ? '2.56%':null)"type="submit" class="btn1" [id]="horizontal.items[i].title"click)="changeImage(id);">

    horizontal.items[i].title
    </button>
    </ng-container>
    </ng-container>

    <div [ngClass]="imgDefault"></div>


    Link:- https://stackblitz.com/edit/angular6-sqemmd?file=app/app.component.html







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 5:57

























    answered Nov 11 '18 at 16:10









    MahiMahi

    745320




    745320












    • Instead of <ng-container>. You should use <div>

      – Mahi
      Nov 11 '18 at 16:13











    • Thanks for your reply. I tried the same. But Im not able to get the image.

      – sai
      Nov 11 '18 at 16:46











    • Is this.imgDefault is getting changed onclick?

      – Mahi
      Nov 11 '18 at 16:51











    • Yes, It needs to be changed on onclick of the button

      – sai
      Nov 11 '18 at 17:01











    • Inspect the div and check whether image is getting loaded on network tab

      – Mahi
      Nov 11 '18 at 17:03

















    • Instead of <ng-container>. You should use <div>

      – Mahi
      Nov 11 '18 at 16:13











    • Thanks for your reply. I tried the same. But Im not able to get the image.

      – sai
      Nov 11 '18 at 16:46











    • Is this.imgDefault is getting changed onclick?

      – Mahi
      Nov 11 '18 at 16:51











    • Yes, It needs to be changed on onclick of the button

      – sai
      Nov 11 '18 at 17:01











    • Inspect the div and check whether image is getting loaded on network tab

      – Mahi
      Nov 11 '18 at 17:03
















    Instead of <ng-container>. You should use <div>

    – Mahi
    Nov 11 '18 at 16:13





    Instead of <ng-container>. You should use <div>

    – Mahi
    Nov 11 '18 at 16:13













    Thanks for your reply. I tried the same. But Im not able to get the image.

    – sai
    Nov 11 '18 at 16:46





    Thanks for your reply. I tried the same. But Im not able to get the image.

    – sai
    Nov 11 '18 at 16:46













    Is this.imgDefault is getting changed onclick?

    – Mahi
    Nov 11 '18 at 16:51





    Is this.imgDefault is getting changed onclick?

    – Mahi
    Nov 11 '18 at 16:51













    Yes, It needs to be changed on onclick of the button

    – sai
    Nov 11 '18 at 17:01





    Yes, It needs to be changed on onclick of the button

    – sai
    Nov 11 '18 at 17:01













    Inspect the div and check whether image is getting loaded on network tab

    – Mahi
    Nov 11 '18 at 17:03





    Inspect the div and check whether image is getting loaded on network tab

    – Mahi
    Nov 11 '18 at 17:03













    0














    You can directly add class names based on the conditions - you are trying to add images from css replace the img tag as div which will render background image based on the class applied



    Make sure if you want your images in the loop, if not move your div outside *ngFor



    <div [class.imgChange]="imgDefault === 'imgChange'" 
    [class.imgChange1]="imgDefault === 'imgChange1'"
    [class.imgChange2]="imgDefault === 'imgChange2'" ></div>


    This will add all your classes based on the condition and the div will render the img - Hope this works Happy coding !!






    share|improve this answer





























      0














      You can directly add class names based on the conditions - you are trying to add images from css replace the img tag as div which will render background image based on the class applied



      Make sure if you want your images in the loop, if not move your div outside *ngFor



      <div [class.imgChange]="imgDefault === 'imgChange'" 
      [class.imgChange1]="imgDefault === 'imgChange1'"
      [class.imgChange2]="imgDefault === 'imgChange2'" ></div>


      This will add all your classes based on the condition and the div will render the img - Hope this works Happy coding !!






      share|improve this answer



























        0












        0








        0







        You can directly add class names based on the conditions - you are trying to add images from css replace the img tag as div which will render background image based on the class applied



        Make sure if you want your images in the loop, if not move your div outside *ngFor



        <div [class.imgChange]="imgDefault === 'imgChange'" 
        [class.imgChange1]="imgDefault === 'imgChange1'"
        [class.imgChange2]="imgDefault === 'imgChange2'" ></div>


        This will add all your classes based on the condition and the div will render the img - Hope this works Happy coding !!






        share|improve this answer















        You can directly add class names based on the conditions - you are trying to add images from css replace the img tag as div which will render background image based on the class applied



        Make sure if you want your images in the loop, if not move your div outside *ngFor



        <div [class.imgChange]="imgDefault === 'imgChange'" 
        [class.imgChange1]="imgDefault === 'imgChange1'"
        [class.imgChange2]="imgDefault === 'imgChange2'" ></div>


        This will add all your classes based on the condition and the div will render the img - Hope this works Happy coding !!







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 '18 at 18:42

























        answered Nov 11 '18 at 18:37









        RahulRahul

        1,0331315




        1,0331315



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250498%2fhow-to-give-condition-in-image-tag-using-angular-6%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)