[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 661
  • Last Modified:

Find parent container from text string

For a prototype, I'm trying to get only a portion of a HTML page and display this to the user.

I know that JQuery will only get local data, so I'm using a proxy.php to get the html.

The result is pretty complex - it's .NET so it contains a lot of extra data - I'm only interested in a part of the data.

The web page is a national vehicle register and I'm tryiong to extract which car model, the registration number entered matches.

var _url = "http://selvbetjening.trafikstyrelsen.dk/Sider/resultater.aspx?Reg="+ regnr;
		$.post("proxy.php",{url:_url},function(_html){
			$("#bil").html( "Server responded..." );
			var _div = $("*:contains('"+ regnr +"')").html();
			console.log( _div ); // Return NULL - should tell me this is a Opel Astra			
		});

Open in new window


You can use this working URL and see the HTML for yourself:
http://selvbetjening.trafikstyrelsen.dk/Sider/resultater.aspx?Reg=XM38315

Any help and pointers would be appreciated
0
internetservice_dk
Asked:
internetservice_dk
  • 5
  • 3
  • 3
1 Solution
 
internetservice_dkAuthor Commented:
Please note that the webpage is in danish ;)
0
 
mcnuteCommented:
Since the values of your query are put into divs with the class .pairValue just add the first and the second .pairValue into your _div variable to combine brand and model to tell you it's an opel astra.

var _url = "http://selvbetjening.trafikstyrelsen.dk/Sider/resultater.aspx?Reg="+ regnr;
		$.post("proxy.php",{url:_url},function(_html){
			$("#bil").html( "Server responded..." );
			var _div = $(".pairValue:eq(0)").html() +' - '+ $(".pairValue:eq(1)").html();
			console.log( _div ); // Return NULL - should tell me this is a Opel Astra			
		});
                                  

Open in new window

0
 
Chris StanyonCommented:
The only way I can think of is to pull in the page, convert it do jQuery elements and then traverse the page.

Once you've got the content, you'll need to find the DIV containing your Reg Num, get it's parent DIV and then return the 2nd <div class=".pairValue">

This assumes that the page you're loading is a fixed format (i.e. your Reg Number will always be in a .pairValue div and the model will always be the 2nd .pairValue div in the parent!

Have a look at this and ask if something doesn't make sense:

var regNo = "XM38315";
url = "loadData.php";

$.post(url,function(html){
	var DOMElements = $(html);
	var model = DOMElements.find("div:contains('" + regNo + "')").parent('div').children('div.pairValue').get(1);
	$('#response').append(model);
});

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
internetservice_dkAuthor Commented:
@mcnute - I cant get your method working, this line

var _div = $(".pairValue:eq(0)").html() +' - '+ $(".pairValue:eq(1)").html();

Shouldn't it look inside _html which holds the result from the server ??

@ChrisStanyon - also can't make your code return the model - I get undefined ?
0
 
Chris StanyonCommented:
Can I see a live link to your site - can't really help much more without it.
0
 
internetservice_dkAuthor Commented:
@Chris... please see this test page at http://drivemagic.nemvagt.dk

And thanks for your time :)
0
 
Chris StanyonCommented:
Works absolute fine for me. Typed in XM38315 and hit enter and it set the text to ASTRA WAGON CLASSIC. Tested in IE, Firefox and Chrome :)
0
 
internetservice_dkAuthor Commented:
Uhh - stupid me - used lowercase xm38315 :(

Thanks for your help, this is perfect :) Will uppercase before submitting!
0
 
mcnuteCommented:
Ah sorry, i oversaw that it is an ajax call.

The way i could think of is to inject the html in your DOM in a hidden div and then access it via jquery.

$.ajax ({
url: http://selvbetjening.trafikstyrelsen.dk/Sider/resultater.aspx,
data: Reg=+regnr,
dataType:html,
success: function(data){
$('#yourhiddendiv').html(data);
}, 
complete: function(){
var carmodel = $(".pairValue:eq(0)").html() +' - '+ $(".pairValue:eq(1)").html();
console.log(carmodel);
}
});

Open in new window

0
 
internetservice_dkAuthor Commented:
@mcnute - thanks for the code, the solution posted by ChrisStanyon worked perfectly so I'll stick to that :)

Thanks for your time and effort!!
0
 
mcnuteCommented:
You're welcome, I misunderstood your question in the first run.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now