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!

How to extract a certain div from responseText

Status
Not open for further replies.

markjohnson123

Programmer
Feb 8, 2009
8
GB
I am just about javascript newbie, so please bear with me!

I have the following code which I use to fetch an external file and place it within a div tag on current page.

At the moment, the code successfully fetches the entire file, but I would like to alter it to fetch only a certain div tag from the file, let's say <div id="mydivtag">

How can I achieve this?

Thanks in advance!

Code:
mnmxmlhttp = new myXMLHttpRequest ();
	if (mnmxmlhttp) {
			mnmxmlhttp.open ("POST", url, true);
			mnmxmlhttp.setRequestHeader ('Content-Type',
					   'application/x-[URL unfurl="true"]www-form-urlencoded');[/URL]

			mnmxmlhttp.send (content);
			errormatch = new RegExp ("^ERROR:");

			target2 = document.getElementById (divcontent);

			mnmxmlhttp.onreadystatechange = function () {
				if (mnmxmlhttp.readyState == 4) {
					mnmString = mnmxmlhttp.responseText;

					if (mnmString.match (errormatch)) {
						mnmString = mnmString.substring (6, mnmString.length);
						target = document.getElementById (divcontent);
						target2.innerHTML = mnmString;
					} else {						
						alert(mnmString);						
						target = document.getElementById (divcontent);
						target2.innerHTML = mnmString;


					}
				}
			}
		}
 
This will require a series of splits...

Take the response and split it by the div tag

Code:
raSplitOne = strResponse.split("<div id=\"mydivtag\">");
);

if the length of raSplitOne is greater than 1, then raSplit[1] will be everything after the div tag.

Take that second part and split it by where you want to finish (probably another div)

Code:
raSplitTwo = raSplitOne("</div>");

Once executed, raSplitTwo[0] will be everything you need.

The only issue with this method is that if there are div tags within the "myDivTag" the split won't work. You'll need to find a unique place to finish it (sometimes there are comments such as <!-- end myDivTag --> where it closes.





TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
^ Sorry,

raSplitTwo = raSplitOne("</div>");

should read

raSplitTwo = raSplitOne[1].split("</div>");



TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top