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.
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
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
});
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
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
});
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@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 ?
var _div = $(".pairValue:eq(0)").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.
ASKER
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 :)
ASKER
Uhh - stupid me - used lowercase xm38315 :(
Thanks for your help, this is perfect :) Will uppercase before submitting!
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.
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);
}
});
ASKER
@mcnute - thanks for the code, the solution posted by ChrisStanyon worked perfectly so I'll stick to that :)
Thanks for your time and effort!!
Thanks for your time and effort!!
You're welcome, I misunderstood your question in the first run.
ASKER