I want to insert different url’s dynamically after checking a condition
Here’s the pseudocode
If div.id = "abc" then
create url <a href="www.zzz.html"'>Cosmetics</a>');
Append url to div
else if div.id = "xyz" then
create url <a href="www.abc.html"'>LEather goods</a>');
Append url to div
else if div.id = "mno" then
create url <a href="www.kkk.html"'>Diapers</a>');
Append url to div
and so on.
How can I write this in JavaScript/jQUery. How to handle the code efficiently if there are 15 such If-Else conditions.
This code is not looking good
if $("#abc) then
var dynLink = $('<a href=zzz.html"'>Cosmetics</a>');
$("#abc").append(dynLink);
else if $("#xyz")
var dynLink = $('<a href=abc.html"'>LEather</a>');
$("#xyz").append(dynLink);
Thanks!
You could try this:
Example: http://jsfiddle.net/Yvuvh/5/
This is just simply setting up the links and titles in the array, and a
forloop is searching through the array to see if it can find adivwith any of thoseid‘s, and if so, then append the appropriate link.To add more, just add another
arrayto thelinkarray, like so: