Structural and electronic properties of co-sputtered Ni-Mg thin films with varying Ni to Mg ratio were studied by in-situ x-ray absorption spectroscopy in the Ni L-edge and Mg K-edge regions. Co-deposition of the metals led to increased disorder and decreased coordination around Ni and Mg compared to pure metal films. Exposure of the metallic films to hydrogen resulted in formation of hydrides and increased disorder. The presence of hydrogen as a near neighbor around Mg caused a drastic reduction in the intensities of multiple scattering resonances at higher energies. The optical switching behavior and changes in the x-ray spectra varied with Ni to Mg atomic ratio. Pure Mg films with Pd overlayers were converted to MgH2: the H atoms occupy regular sites as in bulk MgH2. Although optical switching was slow in the absence of Ni, the amount of H2 absorption was large. Incorporation of Ni in Mg films led to an increase in the speed of optical switching but decreased maximum transparency. Significant shifts in the Ni L3 and L2 peaks are consistent with strong interaction with hydrogen in the mixed films.