Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    44
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,379
    Thanks
    11
    Thanked 592 Times in 572 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    44
    Thanks
    5
    Thanked 0 Times in 0 Posts
    edit nevermind i get it thanks alot
    Last edited by jordandap; 07-18-2011 at 08:03 PM.

  • #4
    New Coder
    Join Date
    Jul 2011
    Posts
    44
    Thanks
    5
    Thanked 0 Times in 0 Posts
    hey it worked but it didnt get rid of the "(Hint: S__)" is there anyway of triming this off?

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,379
    Thanks
    11
    Thanked 592 Times in 572 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • Users who have thanked rnd me for this post:

    jordandap (07-19-2011)

  • #6
    New Coder
    Join Date
    Jul 2011
    Posts
    44
    Thanks
    5
    Thanked 0 Times in 0 Posts
    i did actualy coment what i wanted

    <snip><!--store from here-->What did David Stirling find?<!--to here in a var--> (Hint: S__)<snip>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •