1

I have a javascript array, (not jquery) which I can pull the code needed for src:

var imp = code1 [5];
document.getElementById("prodimg").src=imp; 

with HTML of

<img  id="prodimg">

I am trying to set up a system where pressing code2 will bring up the picture for code 2 etc. So the var imp, will change to code2 [5] when code 2 is clicked.

I have it working for some other data on click, but I am having issues writing to get the specific array back into the img tag.

I was thinking along the lines of

  function changeimg(code)

or

 function changeimg (code, array)

function changeimg (code, [5])
{
  imp = code [5]
}

But its not working and Im sure there is an easier way

Js fiddle: http://jsfiddle.net/9aUq8 with some broken function efforts where each button would change the src of the image to its own array

Or http://jsfiddle.net/9aUq8/1/ shows number 1, and the array for B[2] is the number 2. What function would bring up b[2] showing the number 2 instead of 1.

6
  • 1
    Is the position you want always 5 through multiple arrays? I see you have code1[5] and code2[5] -- these are 2 different arrays at position 5. Commented May 30, 2013 at 18:00
  • 2
    Can you create a fiddle to demonstrate? It's really hard to follow what you are saying. Commented May 30, 2013 at 18:01
  • yes always the same array position, from multiple arrays. Something along the lines of jsfiddle.net/9aUq8 where each button changes the img Commented May 30, 2013 at 18:29
  • jsfiddle.net/9aUq8/1 has my functions that dont work taken out, and maybe a clearer idea. Click B and the pic showing 2 appears Commented May 30, 2013 at 18:43
  • That Fiddle results only in a lot of Uncaught ReferenceError: x is not defined messages in the console. Commented May 30, 2013 at 18:49

2 Answers 2

1

Try use the window['name_array'][index] like in the function from this example.

<script>
var code1 = [1, 2, 3, 4, 5];
var code2 = ['a', 'b', 'c', 'd'];
function setCodeSrc(code, ix) {
  document.getElementById("prodimg").src = window[code][ix];
}

setCodeSrc('code1', 3);
</script>

Or better, the arrays into an object.

<script>
var codes = {
  'code1': [1, 2, 3, 4, 5],
  'code2': ['a', 'b', 'c', 'd']
};

function setCodeSrc(code, ix) {
  document.getElementById("prodimg").src = codes[code][ix];
}

setCodeSrc('code2', 2);
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

thanks - Im slow at JS but it looks interesting. But yes I think the 1st example is what I was looking for. The ix is generic?
The ix is not generic, it is just a property /variable name.
yes sorry, what I meant to say that the ix is not an absolute javascript variable, but a relative one created for the function, and can be called anything?
0

CoursesWeb is prob a lot better but I did it like this:

    function displayArrayAsImage( array ) {

  var picurl = array[2];

  var html = 'img/';    

    html +=  picurl ;

  html += '.jpg';

  return html;
}

 var getimg = document.getElementById("prodimg");

//inset image at load

   getimg.src = displayArrayAsImage (code1); 

//change image on click

function change(code)
{

    getimg.src = displayArrayAsImage (code); 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.