I am trying to have the users avatar change based on what account they have selected from a dropdown box. I wrote some jQuery that I think should work as I am using it for a similar application to do the same thing ( with facebook urls not twitter )
<select id="twid-4646464" onChange="updateTwPic(4646464);">
<option value="billpull-1">BillPull</option>
<option value="billpull1-2">BillPull1</option>
</select>
function updateTwPic(postid){
var twhandle = $('#twid-'+postid).val().split('-');
twhandle = twhandle[0];
var picurl = "http://api.twitter.com/1/users/profile_image/"+twhandle;
$('#twpic-'+postid).attr('src',picurl);
}
Your code works fine if set up correctly: http://jsfiddle.net/86nfP/
Note that I changed
onDomReadytono wrap (head).So what went wrong?
updateTwPicwas defined not in the global scope but in thereadyevent handler, so JavaScript could not find it.If you have had a look at the console, you would have seen:
You could simply attach the event handler with jQuery to avoid such problems:
You could also make use of
data-*attributes, which would be even cleaner:and in the event handler: