Texture 1 Texture 2 Texture 3 Texture 4

Button

<span onclick="setMaterialTexture('1.jpg')" > Texture 1</span>
<span onclick="setMaterialTexture('2.jpg')" > Texture 2</span>

Function

function setMaterialTexture(name)
{
  var space=view3d.space;
  if(space)
  {
    var mtl=space.getMaterial("MyMaterial");    
     if(mtl)
     {
        var diffuse=mtl.diffuse;
        if(diffuse)
        for(var i=0;i<diffuse.length;i++)
	{
	   var channel=diffuse[i];
	   if(channel.texture)
		{
		 var txt=channel.texture;
		 if(txt.ivfile!=name)
                   {
		     mtl.invalidate();
		     txt.ivrefcount--;
                     channel.texture=view3d.space.getTexture(name,view3d.gl.TEXTURE_2D,mtl);
                     view3d.invalidate();
                     return;
		   }
                }
	}
     }
  }
}     

Description

Main logic is implemented in setMaterialTexture function.
It checks for existance of 3d space and locates required 3d material. In this case name of material is "MyMaterial". Of course, name may be different and may be passed as parameter.
If material was found, then function locates texture in diffuse channel - there are may be many many components in single channel.
Next part removes attached texture and attaches new one.

Download

Finalmesh 3d file, texture, html file and javascript files. example.zip