Skip to content Skip to sidebar Skip to footer

Read A Div From An Html File To A Javascript Var

I want to read the colTwo div from the file members.html (in the same folder) into a variable, then set innerHTML to that variable. The result is 'undefined'. How can I fix this? v

Solution 1:

based on your markup : <li><a href="#" onclick='fredpage()'>Fred</a></li>

Try this -

functionfredpage(){
   $.get("members.html", function(data){
     $('#boldStuff').html($(data).find('#colTwo').html());
   }); 
}

Solution 2:

You can simply do this:

$.get("members.html", function (data) {
    $('#boldStuff').html($(data).find('#colTwo').html());
});

Solution 3:

This should work:

var fredpagevar;
$.get("members.html", function(data){
     fredpagevar = $(data).find('#colTwo').html();
}).done(function (){
    document.getElementById('boldStuff').innerHTML = fredpagevar;
});

Your function probably fired before get method receive data from file.


Or second usage (in case when you use your function in other place):
var fredpagevar;
$.get("members.html", function(data){
     fredpagevar = $(data).find('#colTwo').html();
}).done(fredpage());

functionfredpage(){
    document.getElementById('boldStuff').innerHTML = fredpagevar;
}

Post a Comment for "Read A Div From An Html File To A Javascript Var"