Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-17-2011, 10:49 AM   PM User | #1
jordandap
New Coder

 
Join Date: Jul 2011
Posts: 44
Thanks: 5
Thanked 0 Times in 0 Posts
jordandap is an unknown quantity at this point
stripping text from a div and storing into var

well what i want to do is strip the following divs of only some of its information so its easier for the script i have made to complete the answer.

Code:
<div style="display: none;" id="questiondiv">You challenge The Wiseman to a game of trivia, he asks you this question:<br/>&nbsp;<br/>
               <!--store from here-->What did David Stirling find?<!--to here in a var--> (Hint: S__)<br/>&nbsp;<br/>
                <input class="dungeoninput" type="text" size="16" id="answer" value="" maxlength="32"></input><br/>
                <div class="btn100" style="margin-top: 6px;"
				onclick="loadDiv2('/dream/explore?action=acceptTrivia&qid=727'); return false;">Answer</div>
                </div>
but it cannot edit the html in anyway(on load edit) but edit it trough firefox greasemonkey preferably. i would be very greatfull for anyone who can help because i know some things about javascript but when it comes to stripping content my brain just fries, and if you need more explanation just ask.

Last edited by jordandap; 07-17-2011 at 10:50 AM.. Reason: changing [html][/html] tags for [code][/code] tags
jordandap is offline   Reply With Quote
Old 07-18-2011, 12:22 AM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
scraping can be tricky, especially with text segments.
i often use my handy node getter for these types of situations.

indeed, it seemed to work like a charm on the markup you posted:

Code:
<body>
 <div style="display: none;" id="questiondiv">
You challenge The Wiseman to a game of trivia, he asks you this question:<br/>&nbsp;<br/>
               <!--store from here-->What did David Stirling find?<!--to here in a var--> (Hint: S__)<br/>&nbsp;<br/>
                <input class="dungeoninput" type="text" size="16" id="answer" value="" maxlength="32"></input><br/>
                <div class="btn100" style="margin-top: 6px;"
				onclick="loadDiv2('/dream/explore?action=acceptTrivia&qid=727'); return false;">Answer</div>
                </div>









<script>
function getNodes(prop, val, meth, nd, useSelf ){
 var r=[], any= getNodes[val]===true;
 nd=nd||document.documentElement;
 if(nd.constructor===Array){nd={childNodes:nd};}
  for(var cn=nd.childNodes, i=0, mx=cn.length;i<mx;i++){
    var it=cn[i];
    if( it.childNodes.length && !useSelf ){r=r.concat(getNodes(prop, val, meth, it,useSelf ));}
	if( any ? it[prop] : (it[prop]!==undefined && (meth ? ""[meth] && 
		String(it[prop])[meth](val) : it[prop]==val))){
		  r[r.length]=it; 
	}
  }//nxt
 
return r;
};getNodes[null]=true;getNodes[undefined]=true;
//end getNodes()






//example: 	(modify arguments to suit your needs)
alert(
  getNodes(
	"data",	//examine each node's .data property (only hits text nodes)
	/\?/,		//looking for text with a "?"
	"match",	//using String().match method to accept arguments[1]
	document.getElementById("questiondiv") // looking in the question div
  )[0].textContent 	//show text of first match
);



</script>
</body>
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 07-18-2011, 07:55 PM   PM User | #3
jordandap
New Coder

 
Join Date: Jul 2011
Posts: 44
Thanks: 5
Thanked 0 Times in 0 Posts
jordandap is an unknown quantity at this point
edit nevermind i get it thanks alot

Last edited by jordandap; 07-18-2011 at 08:03 PM..
jordandap is offline   Reply With Quote
Old 07-18-2011, 08:51 PM   PM User | #4
jordandap
New Coder

 
Join Date: Jul 2011
Posts: 44
Thanks: 5
Thanked 0 Times in 0 Posts
jordandap is an unknown quantity at this point
hey it worked but it didnt get rid of the "(Hint: S__)" is there anyway of triming this off?
jordandap is offline   Reply With Quote
Old 07-18-2011, 09:16 PM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by jordandap View Post
hey it worked but it didnt get rid of the "(Hint: S__)" is there anyway of triming this off?
then your example was different than the actual source. tisk tisk...

at any rate, just use a split:

Code:
//example: 	(modify arguments to suit your needs)
alert(
  getNodes(
	"data",	//examine each node's .data property (only hits text nodes)
	/\?/,		//looking for text with a "?"
	"match",	//using String().match method to accept arguments[1]
	document.getElementById("questiondiv") // looking in the question div
  )[0].textContent.split("?")[0]+"?"	//show text of first match
);
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
jordandap (07-19-2011)
Old 07-18-2011, 09:32 PM   PM User | #6
jordandap
New Coder

 
Join Date: Jul 2011
Posts: 44
Thanks: 5
Thanked 0 Times in 0 Posts
jordandap is an unknown quantity at this point
i did actualy coment what i wanted

<snip><!--store from here-->What did David Stirling find?<!--to here in a var--> (Hint: S__)<snip>
jordandap is offline   Reply With Quote
Reply

Bookmarks

Tags
div, grease monkey, greasemonkey, javascript, variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:07 PM.


Advertisement
Log in to turn off these ads.