Link to home
Start Free TrialLog in
Avatar of internetservice_dk
internetservice_dk

asked on

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
Avatar of internetservice_dk
internetservice_dk

ASKER

Please note that the webpage is in danish ;)
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

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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 ?
Can I see a live link to your site - can't really help much more without it.
@Chris... please see this test page at http://drivemagic.nemvagt.dk

And thanks for your time :)
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 :)
Uhh - stupid me - used lowercase xm38315 :(

Thanks for your help, this is perfect :) Will uppercase before submitting!
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

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

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