3

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

Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
ha chitti
  • 139
  • 2
  • 11

3 Answers3

5

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

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
1
<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();
}
Muhammad Usman
  • 1,366
  • 5
  • 18
  • 33
0

You can pass the current element in the function using that you can locate the span element which is a children on the a tag

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

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

function DoSomething(node) {
   var htmlVal = "";
   skuList  = $(node).find('span').html();//for children
   //skuList  = $(node).next('span').html();//for siblings
}
joyBlanks
  • 6,419
  • 1
  • 22
  • 47