0

I have 4 <SPAN> ,out of which 3 are hidden (display:none) in dynamic gridview

<tr>
   <SPAN id="Span1">Name</SPAN>
   <SPAN id="Span2" style="display:none"></SPAN>
   Id1<SPAN id="Span3" style="display:none">Id2</SPAN>
   <SPAN id="Span3" style="display:none">Id3</SPAN>
</td>

How could I get the text of those? I am getting that element in alert as [object HtmlSpanElement]. I had written it using innerHtml & innerText but result undefined.

Dennis Röttger
  • 1,975
  • 6
  • 32
  • 51
MayureshP
  • 2,514
  • 6
  • 31
  • 41
  • 3
    Tip, id's can't start with a numeric char – Ivo Jun 14 '11 at 06:47
  • This is shortcode. i am having too long character id's – MayureshP Jun 14 '11 at 07:01
  • Your sample code shows 3 spans, not the 4 you said you have. They're also not styled as display:none. You haven't shown the code you're using to get the [object HtmlSpanElement] either. Please revise your question. – Bernhard Hofmann Jun 14 '11 at 07:11
  • @Bernhard Hofmann: I created gridview in some other .cs file, everything is dynamic. Thats why i written very clearly in question.this is shortcode. Try to tell me answer,why its not working in Mozilla – MayureshP Jun 14 '11 at 07:25
  • why have you tagged this with c#? it seems this is not a c# question – kͩeͣmͮpͥ ͩ Jun 14 '11 at 08:08

6 Answers6

2

As requested by poster, and with thanks to V4Vendetta

The code to get the text content of a none is:

var content = span.innerText || span.textContent;

This works because (in general) those browsers that don't support innerText do support textContent. quirksmode has more detail.

kͩeͣmͮpͥ ͩ
  • 7,783
  • 26
  • 40
0

Using JQuery, this would get the values:

$(function() {
   $('tr span').each(function() {
     alert($(this).text());
   });
});

Here's an example

Nathan
  • 6,095
  • 2
  • 35
  • 61
0

USE:

var str = document.getElementById('span1').value;
kapa
  • 77,694
  • 21
  • 158
  • 175
Saurabh
  • 5,661
  • 2
  • 26
  • 32
0

Some browsers(Firefox) do not support innerText and would return undefined in such cases Read This

One more way would be to check textContent but then its not suppoerted in IE

So i would suggest you to try innerHTML and not innerHtml (you have to be careful of the case) and this should work in all the browsers.

Community
  • 1
  • 1
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
0

Give your spans the Attribute runat="server" and access them like Span1.InnerHtml, Span2.InnerHtml, Span3.InnerHtml, etc. in CodeBehind. Or, considering that you get values from GridView, ((HtmlGenericControl)grid.rows[x].cells[y].FindControl("yourSpanId")).InnerHtml.

Dennis Röttger
  • 1,975
  • 6
  • 32
  • 51
0
<script language="javascript" type="text/javascript"> 
 function MyFunction(){
     var gridview = document.getElementById('ctl00_ContentPlaceHolder1_gvmanageclients'); 
            var rCount = gridview.rows.length;
            alert(gridview.rows.length);
            var rowIndex=1;

            for (rowIndex; rowIndex<=rCount-1; rowIndex++)
            {
                var rowElement = gridview.rows[rowIndex];
                Cell = gridview.rows[rowIndex].cells[0];
                FirstControl = Cell.childNodes[0];
                alert(FirstControl.innerHTML);  

             }

  }
 </script>
Saurabh
  • 5,661
  • 2
  • 26
  • 32