0

Thanks in advance, I'm trying to create an if statement that will help me solve an issue. I want to be able to assign an ID for a button and if that ID matches the "if statement id entered" it will run my script else it wont do nothing. i'v tried couple of things, but with no luck, I'm new to jQuery hope someone could give a hint,any other suggestion will be great ,cheers.

this is the script I want to run:

jQuery("#myId").append("<i class='fa fa-camera-retro fa-lg'></i>");

this is what iv tried:

if($('a' == '#myId') {
    jQuery("#myId").append("<i class='fa fa-camera-retro fa-lg'></i>");
} 
​

thanks to all

osherez
  • 99
  • 1
  • 13
  • you want to check `#myId` exists? – zabusa Dec 18 '17 at 12:50
  • Please try to describe more with the relevant HTML code, so we could understand the context... – Zakaria Acharki Dec 18 '17 at 12:51
  • are you looking like this? https://jsfiddle.net/xzmu04sq/2/ – Suresh Ponnukalai Dec 18 '17 at 13:04
  • Hey guys sorry if i want clear. il try to explain more. I'm using divi theme word press. and I cannot add font awesome to buttons even with css. So i figured maybe its possible to do so with jquery and its working great the only thing left to do is to run the script only if I assign a certain id to a button if not the script wont fire. this is the Html of the button - without font awesome inserted `font awesome button` – osherez Dec 18 '17 at 13:08
  • Thank you all @riot Mentioned the `$('a').is('#thatID')` working great thanks to all appreciate the help :) – osherez Dec 18 '17 at 13:22

1 Answers1

1

Since I don't know when you would like to fire that piece of code: if you are trying to trigger it after an event being dispatched you can Simply use the .on() function to attach the event listener to the element. If you are Just iterating over an element list you can use the .is() function, and check if that $('a').is('#thatID') !

riot
  • 76
  • 8
  • Hey guys sorry if i want clear. il try to explain more. I'm using divi theme word press. and I cannot add font awesome to buttons even with css. So i figured maybe its possible to do so with jquery and its working great the only thing left to do is to run the script only if I assign a certain id to a button if not the script wont fire. this is the Html of the button - without font awesome inserted `font awesome button` – osherez Dec 18 '17 at 13:12
  • You want to use a class instead of an ID, and just do it using the `.each()` function – riot Dec 18 '17 at 13:15
  • Hey @riot thanks man working great appreciate the help iv used it with the `$('a').is('#thatID')` Cheers have a great day :) – osherez Dec 18 '17 at 13:24