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>
Share
Improve this question
edited Jul 29, 2015 at 9:43
michelem
14.6k5 gold badges55 silver badges67 bronze badges
asked Jul 29, 2015 at 9:07
Debabrata RoyDebabrata Roy
311 gold badge1 silver badge5 bronze badges
5
- actually I din't get it – Francesco Commented Jul 29, 2015 at 9:12
- I don't understand at all what you want. – thomasb Commented Jul 29, 2015 at 9:17
- If you can, try and elaborate what you want, also include any attempts you've made to solve this issue yourself. (Edit your question and include the code there.) – Epodax Commented Jul 29, 2015 at 9:18
- he's assuming a function in js can only accept one parameter – Veverke Commented Jul 29, 2015 at 9:18
- send 2 ids to WHAT ? – 3pic Commented Jul 29, 2015 at 9:20
3 Answers
Reset to default 1You 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!");
});