views:

7172

answers:

3

Hi, i'm relatively new to jquery and javascript and am trying to pass a unique id (number) into a flickr search function (jquery.flickr-1.0-js) like so, (number is the variable where i'm storing the unique id)

<script type="text/javascript" src="javascripts/jquery.flickr-1.0.js"></script>

 <script type="text/javascript"> 

 jQuery(function(){   
     jQuery(".btnRefresh").click(function(){
       var number = $(this).attr("id");
       $('#gallery_flickr_'+number+'').show();
      jQuery('#gallery_flickr_'+number+'').html("").flickr({      
       api_key: "XXXXXXXXXXXXXXXXXXXXXXX",     
       per_page: 15   
      });
     }); 
    }); 

  </script>

When i try to pass it in to the flickr-1.0.js function like so (abbreviated)

(function($) {
$.fn.flickr = function(o){

var s = {
    text: $('input#flickr_search_'+number+'').val(),
   };
  };
})(jQuery);

i get a error

number is not defined [Break on this error] text: $('input#flickr_search_'+numbe...] for type=='search' free text search

please help, what do i need to do to pass the variable between the two scripts?

Thanks,

+1  A: 

Try adjusting your scripts as below:

 ...
            jQuery('#gallery_flickr_'+number+'').html("").flickr({      
                    api_key: "XXXXXXXXXXXXXXXXXXXXXXX",     
                    per_page: 15,
                    search_text: $('input#flickr_search_'+number+'').val()  
            });
...



(function($) {
$.fn.flickr = function(o){

var s = {
    text: o.search_text
   };
  };
})(jQuery);

The idea is that you need to find the search text based on the id of the clicked item. You do that in the function where the id is available, then pass the value of the input to the flickr function with the other values in the hash argument. In the flickr function you extract the named item from the hash and use the value.

tvanfosson
A: 

I think i'm close now, however instead of an error as before, now the 'text' parameter is coming back blank, when i try and execute the search,

jQuery(function(){   
 jQuery(".btnRefresh").click(function(){
   var number = $(this).attr("id");
   $('#gallery_flickr_'+number+'').show();
  jQuery('#gallery_flickr_'+number+'').html("").flickr({      
   api_key: "XXXXXXXXXXXXXXXXXXXX",     
   per_page: 15,
   search_text: $('input#flicker_search_'+number+'').val() 
  });
 }); 
});
(function($) {
$.fn.flickr = function(o){

var s = {
    text: o.search_text
   };
  };
})(jQuery);

can you explain where 'o.search_text' comes from?

Thanks

adam
The "o" is the formal parameter of the flickr function. You are constructing it in the first function when you create the hash containing the api_key, per_page, and search_text keys.
tvanfosson
A: 

I just realized 'flicker' != 'flickr'

damn you cutesy web 2.0 names!!!

Thanks!

adam
Sorry about that.
tvanfosson
I corrected my post. BTW, the way stackoverflow works, you should upvote helpful answers and click the accept arrow on the post that best answers your question.
tvanfosson
Generally, you want to update/edit your question rather than providing new information in an answer. Answers are supposed to be just that: answers, not additions to the question.
tvanfosson