0

I have an image map and i want to have the x1,y1,x2,y2 separate in different variables, this is the map:

    <area shape="rect"
    coords="470,555,863,665"
    id="map"
    href="#"

this is how to get the coords out of it:

 var myVar = (document.getElementById("map").coords);

but the output of this line is "470,555,863,665" is it possible to separate them into different variables?

thanks

Floris497
  • 1,406
  • 12
  • 18

2 Answers2

2
var coords = myVar.split(",");

coords is now an array of the individual values in the comma separated list. So x1 = coords[0], y1 = coords[1], etc.

Vala
  • 5,628
  • 1
  • 29
  • 55
  • it does not work. var myVar = document.getElementById("mapr").coords) var coords = myVar.split(","); var ht = coords[0]; var bt = coords[1]; the function is not working if i fill in ht = 70; it works did i did something wrong? – Floris497 Jul 18 '12 at 11:44
  • What error are you getting? I don't see a ; between setting myVar and coords there, but that could just be the comment formatting. Other than that I'd expect the split call to work if your myVar has the correct value. – Vala Jul 18 '12 at 11:49
  • the ; sign was not the problem. but when i put 70 in ht it does not work until i delete these two lines: var myVar = document.getElementById("mapr").coords); var coords = myVar.split(","); – Floris497 Jul 18 '12 at 11:54
  • I'm not sure what you mean by putting 70 in ht. The first reference to 'ht' I've seen here is when you put `ht = coords[0]`. What's the value of `myVar` before you do `split`? – Vala Jul 18 '12 at 11:56
  • "470,555,863,665" this is before the split by ht is a var and 70 is a random number to test the rest of the script. – Floris497 Jul 18 '12 at 11:59
  • i do not get any error message were can i find error messages? – Floris497 Jul 18 '12 at 12:09
  • `"470,555,863,665".split(",")` provides an array `[470,555,863,665]` as expected. I think the error must be elsewhere. It's a bit tricky to debug without more code / error information. To get error messages - if there are any - you'll need to open up the JS console. It depends on your browser how to do that, it's CTRL+SHIFT+J in Chrome, not sure about the rest. – Vala Jul 18 '12 at 12:24
  • coords[1] contains the right number but somehow the script wont read it properly. – Floris497 Jul 18 '12 at 13:17
1
var myVar = (document.getElementById("map").coords).split(',');
//myVar will be an array of all those values
coolguy
  • 7,866
  • 9
  • 45
  • 71