loriensleafs
loriensleafs

Reputation: 2255

using jquery load to make a variable

I have a jQuery .load() function that finds a specific DIV in another page, I'd like to store the ID of that DIV into a variable, but I'm not entirely sure how to do it.

$('#bow_1_img').load('http://www.klossal.com/klossviolins/inventory.html .bow:first');

so this would load the contents of the first div with the class bow, .bow:first, into #bow_1_img but instead I'd like to load it's ID into a variable, any idea on how to do that?

$.get('http://www.klossal.com/klossviolins/inventory.html', function(data) {
    var elem = $(data).find('.bow:first'),
          bow_id = elem.attr('id');
}); 
$("#bow_1").click(function() {
        window.location.href = "http://www.klossal.com/klossviolins/inventory.html#" + bow_id;
}); 

Upvotes: 0

Views: 89

Answers (1)

adeneo
adeneo

Reputation: 318342

load() is just a shortcut for $.get, if you only need the ID, use $.get

$.get('http://www.klossal.com/klossviolins/inventory.html', function(data) {
    var elem = $(data).find('.bow:first'),
          ID = elem.attr('id');
     $("#bow_1").click(function() {
        window.location.href = "http://www.klossal.com/klossviolins/inventory.html#" + bow_id;
     });
});

remember that it's async, so ID will only be available after the ajax function completes.

Upvotes: 3

Related Questions