0

I'm trying to get spans text by passing the value of span id to the function like this :

function getDetails(testDetailId){
alert(jq("#"+testDetailId).text());
}

And I always get null alerted when the click is invoked :

jq("table tr").click(function(){
var testDetailId = jq(this).attr("id");
getDetails(testDetailId);
}); 

I use jquery no conflict :

var jq = jQuery.noConflict();

some html :

<span id="2">[{"testId":22,"testPerfReports":[]}]</span>

Any ideas ?

London
  • 14,986
  • 35
  • 106
  • 147
  • It would help to see your mark-up. – David Thomas Oct 21 '11 at 16:07
  • Your code is a bit strange... why do you have this extra `getDetails` function? You already have a reference to the element, `this`, so you could just do `jq(this).text()` inside the event handler (if this is what you want, which I doubt somehow, but that is the same as what you are doing). – Felix Kling Oct 21 '11 at 16:11
  • 1
    As an aside, 2 is an invalid id name. http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html – ScottE Oct 21 '11 at 16:13
  • @Felix Kling span is not in the table at all, if that is what you're refering to – London Oct 21 '11 at 16:14
  • @ScottE good catch I'll try to alter the code add a letter before – London Oct 21 '11 at 16:16
  • @London: Then I assume that your `tr` element either has no ID or you have two elements with the same ID, which is invalid. Otherwise your code does not make sense. Please create a http://jsfiddle.net/ demo. – Felix Kling Oct 21 '11 at 16:18
  • 1
    @ScottE: IDs starting with numbers are allowed in HTML5. – Felix Kling Oct 21 '11 at 16:18
  • @Felix Kling I checked that the tr id is valid value, at least in firebug – London Oct 21 '11 at 16:19
  • Then both the `tr` element and the `span` have the same id? That could be the problem. As I said, IDs have to be unique. Please create a http://jsfiddle.net/ demo. – Felix Kling Oct 21 '11 at 16:21
  • @Felix Kling God you're smart, yes believe me I know about the one id rule, but working late really got me nowhere. that was it, please answer I'll accept – London Oct 21 '11 at 16:28
  • @London: No worries, for others it is not late yet and can jump in to help you ;) – Felix Kling Oct 21 '11 at 16:32

2 Answers2

1

It appears to me that both, the tr element and the span have the same ID.

That could be the problem, IDs have to be unique.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

That is because you are capturing the click event of the <TR>. As they are part of a table they do not have the text() function. If you are trying to get the text of the span try doing the following:

jq("table tr").click(function(){
    var concatenatedValues = $.map($('jq(this).find('span').text(), function (text) {
                return option.value;
            }).join(',');

    alert(concatenatedValues);
}); 
PCasagrande
  • 5,302
  • 3
  • 27
  • 36