Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call same method with different Ids

I have html controls of two sets with different ids in my page, when I click a tag I want to call DoSomething method.

<div>
   <a href="javascript:void(0);" onclick="DoSomething();">
   <span id="spnValue" >Value1</span>
</div>

<div>
   <a href="javascript:void(0);" onclick="DoSomething();">
   <span id="spnValue1">Value2</span>
</div>

function DoSomething() {
   var htmlVal = "";
   skuList  = $("span[id*='spnValue']").html();
}

But whichever one I click it gives the Value1 in htmlVal. How can I distinguish and retrieve the value of method called

like image 984
ha chitti Avatar asked Sep 12 '26 00:09

ha chitti


2 Answers

You can pass clicked element object to method:

<a href="javascript:void(0);" onclick="DoSomething(this);">
<span id="spnValue1">Value2</span>
</div>

and then use it for traversing to child span element:

function DoSomething(obj) {
  var htmlVal = "";
  skuList  = $(obj).find('span').html();
}

Working Demo

like image 50
Milind Anantwar Avatar answered Sep 13 '26 14:09

Milind Anantwar


<div>
       <a href="javascript:void(0);" onclick="DoSomething(this);">
      <span id="spnValue1">Value2</span>
</div>

function DoSomething(obj) {
    var htmlVal = "";
    skuList  = $(obj).parent().find('span').html();
}
like image 22
Muhammad Usman Avatar answered Sep 13 '26 14:09

Muhammad Usman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!