Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 27th, 2006, 06:56 PM
Registered User
 
Join Date: Nov 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extract text with java script

Hello,
<div id=header>
<span>
<a href=some_page>Welcome</a>
<a href=other_page>Themajor</a>
</span></div>

This was html code generated with aspx where the major is a user name that dynamically changes according to the logged user.
Now i want to write javascript code that gets "themajor" or whatever for me from this html code

 
Old November 28th, 2006, 03:28 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hiii TheMajor!!
Hope this will help you
Since you want to get "TheMajor" ,second approach will be better

<script>
function fun()
{
var obj=document.getElementsByTagName('a')
for(i=0;i<obj.length;i++)
alert(obj[i].innerHTML)
}

function fun2()
{
var obj=document.getElementById('t1')
alert(obj.innerHTML)

var obj=document.getElementById('t2')
alert(obj.innerHTML)
}
</script>
 Approach1:When you want to get All Href innerHTML Values,like(Welcome,Themajor,Welcome2,TheMajor3)
 <br>
<div id=header>
<span>
<a href=some_page>Welcome</a>
<a href=other_page>Themajor</a>
</span></div>



Approach2:When you want to get specific Href innerHTML Values,like when you want to get the value for Welcome2 and TheMajor3
<br>
<div id=header>
<span>
<a href=some_page id="t1">Welcome2</a>
<a href=other_page id="t2">Themajor3</a>
</span></div>

<input type=button value="Approach1" onclick="fun()">
<br><br>
<input type=button value="Approach2" onclick="fun2()">

Cheers :)

vinod
 
Old November 28th, 2006, 08:12 AM
Registered User
 
Join Date: Nov 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thnx for reply, but can i get another approach becoz i need TheMajor like the second approach but i can't change the <a> by giving it an id, so can i write code that extract the second <a> under the <div id=header>?

 
Old November 29th, 2006, 05:48 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Hii TheMajor!!
Offcourse yes,if you are sure that you are getting the value of second<a href> Tag,than only you can succed,becoz after the developement anyother user adds the same <a href>Xyx</a> tag ,before the second one,your code will return the second Xyx values.
If you are sure that no one will modify the code after the release,then use
function fun()
{
var obj=document.getElementsByTagName('a')
alert(obj[1].innerHTML)//second Href Tag Value in ur case The Major
}
Hope this will help you

Cheers :)

vinod
 
Old November 30th, 2006, 10:46 AM
Registered User
 
Join Date: Nov 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, this is helpful

 
Old September 30th, 2007, 09:45 PM
Registered User
 
Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by TheMajor
 Hello,
<div id=header>
<span>
<a href=some_page>Welcome</a>
<a href=other_page>Themajor</a>
</span></div>

This was html code generated with aspx where the major is a user name that dynamically changes according to the logged user.
Now i want to write javascript code that gets "themajor" or whatever for me from this html code

If you want to do this in a generic way then you can use this tool. http://www.unitedwebandstand.com/js/kHTML.js

In your case you could simply do
var r = us.karnivoro.stripHTML((document.getElementById('h eader')).innerHTML);
alert(r);

For a working example see http://www.unitedwebandstand.com/wordpress/?p=90 or http://www.karnivoro.us/a.html






Similar Threads
Thread Thread Starter Forum Replies Last Post
Text to Speech using Java Script on Windows XP sreek_s Other Programming Languages 2 December 23rd, 2007 01:05 PM
how to extract text from html??? naureen Java Basics 2 October 2nd, 2007 11:19 AM
Rich Text Editor in Java Script needed iniro Javascript 4 September 7th, 2004 04:50 AM
Rich Text Editor in Java Script needed iniro HTML Code Clinic 3 September 7th, 2004 03:54 AM
Extract text from text file & put in dropdown box tsukey Beginning PHP 5 July 20th, 2004 09:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.