I want to sen two id's when onclick occurs like now when div 2 is clicked I want to pass just the id of the 2nd div too & then write script for the 2nd div. is it possible? if so, then how? any example?
<div id="1st_div"></div>
<div id="2nd_div">
<div id="inner_of_2nd_div" onclick="myfunction('1st_div')"></div>
</div>
I want to sen two id's when onclick occurs like now when div 2 is clicked I want to pass just the id of the 2nd div too & then write script for the 2nd div. is it possible? if so, then how? any example?
<div id="1st_div"></div>
<div id="2nd_div">
<div id="inner_of_2nd_div" onclick="myfunction('1st_div')"></div>
</div>
You can pass as many parameters as you wish. Just define the function accordingly.
For instance, for a 2 parameters function myFunction, your onclick should look like
onclick="myfunction(p1, p2)"
and your function myFunction like
myfunction(p1, p2) {
//do your stuff
}
<div id="1st_div"></div>
<div id="2nd_div">
<div id="inner_of_2nd_div" onclick="myfunction(document.getElementById('1st_div'));">asa</div>
<script>
function myfunction(obj)
{
//here i caught the object
obj.innerHTML=obj.id;
}
</script>
I understand that you want a function which has id argument of the clicked div. If right, use JQuery and do that:
HTML:
<script src="http://code.jquery./jquery-1.11.3.min.js"/>
<div id="1st_div" class="clickable"></div>
<div id="2nd_div" class="clickable">
<div id="inner_of_2nd_div" ></div>
</div>
JS Jquery:
$(".clickable").click(function(){
var _id=$(this).attr("id");
alert("I am div id="+_id+" and I've been clicked right now!");
});