Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get value of myURL using document.getElementBy().innerHTML 1

Status
Not open for further replies.

lynque

IS-IT--Management
Sep 30, 2004
124
CA
Hello,
I am trying to get the innerHTML of a specific a tag's href on my page.
The a tag has a unique css class
<a class="myClass" href="myURL">

I'd like to do something like this
<script language="javascript">

function hello(){
var foo = document.getElementsByTagName("myClass")[0];
if (foo) alert(foo.innerHTML);
}
</script>

I guess this question has two parts:
Is it possible to use getElementByTagName to access the innerHTML of a css class?
Is it possible to get a specific attribute of said css class and access it's innerHTML?

If it is then any pointers or links to examples would be very much appreciated.

Thanks
 
Hi

lynque said:
[tt]<a [red]class[/red]="myClass" href="myURL">

document.getElementsBy[red]Tag[/red]Name("myClass")[0];[/tt]
tag != class
lynque said:
The a tag has a unique css class
If it is unique, why not make it [tt]id[/tt] ?
Code:
<a [red]id[/red]="myClass" href="myURL">whatever</a>

<script type="text/javascript">
function hello()
{
  var foo = document.getElementBy[red]Id[/red]("myClass");
  if (foo) alert(foo.innerHTML);
}
</script>
If you not care about portability, you may use the [tt]getElementsByClassName()[/tt] method, which is HTML 5's addition, currently handled by Gecko, Presto and WebKit. ( See Comparison of layout engines (HTML 5) | APIs. )
Code:
<a class="myClass" href="myURL">whatever</a>

<script type="text/javascript">
function hello()
{
  var foo = document.getElementsBy[red]Class[/red]Name("myClass")[0];
  if (foo) alert(foo.innerHTML);
}
</script>
Or use XPath :
Code:
<a class="myClass" href="myURL">whatever</a>

<script type="text/javascript">
function hello()
{
  var foo = document.[red]evaluate('//a[@class="myClass"]',document,null,XPathResult.ANY_UNORDERED_NODE_TYPE,null).singleNodeValue[/red]
  if (foo) alert(foo.innerHTML);
}

Feherke.
 
Hi

By the way, if you want to use class and want it portable, write your own function :
Code:
<a class="myClass" href="myURL">whatever</a>

<script type="text/javascript">
function hello()
{
  var foo = [red]mydocumentgetElementsByClassName[/red]("myClass")[0];
  if (foo) alert(foo.innerHTML);
}

[red]function mydocumentgetElementsByClassName(what)
{
  var all=document.getElementsByTagName('*')
  var expression=new RegExp('\\b'+what+'\\b','i')
  var result=[]
  for (var i=0,l=all.length;i<l;i++) if (expression.test(all[i].className)) result.push(all[i])
  return result
}[/red]
</script>

Feherke.
 
Thanks a lot Feherke.
This has pointed me in the right direction.
 
Is there a way to alert href attribute assciated with "myClass"?
 
I figured it out, thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top