I have tried to load html in a div by clicking the links.I managed to do it.But i have another requirements that i need minimal jquery code and when i click each link appropriate html in the div.
html of my code:
<div class="tac templateIcons">
<a href="#" data-id="1" id="template1">
<img src="assets/img/template1-01.png" alt="template 1" />
</a>
<a href="#" data-id="2" id="template2">
<img src="assets/img/template1-02.png" alt="template 2" />
</a>
<a href="#" data-id="3" id="template3">
<img src="assets/img/template1-03.png" alt="template 3" />
</a>
</div>
<div class="contentParent">
<div id="contentDiv">
</div>
</div>
Javascript code
$(function(){
$("#template1").click(function(){
$("#contentDiv").load("template2.html");
});
$("#template2").click(function(){
$("#contentDiv").load("template2.html");
});
$("#template3").click(function(){
$("#contentDiv").load("template3.html");
});
});
The above code is working for me, but the issues is if i have 20 a hrefs or pages i have to write 20 js functions .
I need to avoid it by doing a smart dynamic js solution.
I wrote js code based on the data attribute as follows:
html code:
<div class="tac templateIcons">
<a href="#" data-id="1" data-name="1" id="hidediv">
<img src="assets/img/template1-01.png" alt="template 1" />
</a>
<a href="#" data-id="2" data-name="1" id="hidediv">
<img src="assets/img/template1-02.png" alt="template 2" />
</a>
<a href="#" data-id="3" data-name="1" id="hidediv">
<img src="assets/img/template1-03.png" alt="template 3" />
</a>
</div>
<div class="contentParent">
<div id="contentDiv" class="contentDivStyle">
</div>
</div>
JS code:
$(function(){
$("#hidediv").click(function(){
$(this).attr("data-id");
$page = "template" + $(this).attr("data-id") + ".html";
console.log("log message:" + $page);
$("#contentDiv").load($page);
});
});
But the thing is i used same id for the all the a hrefs and it will take only the first one. How can i make it dynamic even if i add 30 a hrefs and use a single piece of js code?
Could you please help me?