2

I am getting the element position attributes and storing it in a variable. When I print that variable in console I can see that its a object like this:

Object {.portsTool path.aggregate: Object, .portsTool rect.remove: Object, .portsTool .handleInPorts: Object, .portsTool .handleOutPorts: Object, .resizeTool .resize: Object…}.: Object.body: Objectheight: 250stroke: "#000000"width: 150__proto__: Object.inPorts .port-label: Object.inPorts circle: Object.moveTool .area: Object.moveTool .visual: Object.outPorts .port-label: Object.outPorts circle: Object.port-body: Object.portsTool .handleInPorts: Object.portsTool .handleOutPorts: Object.portsTool path.aggregate: Object.portsTool rect.remove: Object.resizeTool .resize: Objectrect: Objecttext: Object__proto__: Object

When I expand it:

>: Object
.body: Object
height: 250
stroke: "#000000"
width: 150
__proto__: Object

I want to get the value of width from this. How do i do that? I am fairly new to javascript

Update:

var border = border2.prop('attrs');
   for (y in border) 
   {
     console.log(border[y]);
   }

Using the above iteration its printing the objects like this:

Object {d: "m0,5l5,0l0,-5l5,0l0,5l5,0l0,5-5,0l0,5l-5,0l0,-5l-5,0z", stroke-width: 2, stroke: "#000", fill: "#5F5"}
(index):527 Object {width: 15, height: 6, stroke-width: 3, stroke: "#000", fill: "#F55"…}
(index):527 Object {ref: ".body", ref-x: -30, ref-y: -40}
(index):527 Object {ref: ".body", ref-dx: 10, ref-y: -40}
(index):527 Object {d: "M 0,10l10,0l0,-10z M -2,13l15,0l0,-14l0,14", fill: "black", stroke: "black", ref: ".body", ref-dx: 4…}
(index):527 Object {d: "M 0,15l5,-3l0,6l-5,-3l30,0l-5,-3l0,6l5,-3l-15,0l0,15l-3,-5l6,0l-3,5l0,-30l-3,5l6,0l-3,-5l0,15", fill: "black", stroke: "black", ref: ".body", ref-x: -40…}
(index):527 Object {ref: ".moveTool .visual", ref-x: 0, ref-y: 0, cx: "15", cy: "15"…}
(index):527 Object {font-size: 14, text: "", ref-x: 0.5, ref-y: 0.5, ref: ".body"…}
(index):527 Object {stroke: "#fff", fill: "#F9F9F9", stroke-width: 1, opacity: 0.8}
(index):527 Object {magnet: false, fill: "#FFFFFF", stroke: "none"}
(index):527 Object {type: "input"}
(index):527 Object {type: "output"}
(index):527 Object {r: 3, magnet: true, stroke: "#000000"}
(index):527 Object {width: 150, height: 250, stroke: "#000000"}
(index):527 Object {x: -15, dy: 4, text-anchor: "end", fill: "#000000"}
(index):527 Object {x: 15, dy: 4, fill: "#000000"}
kittu
  • 6,662
  • 21
  • 91
  • 185

5 Answers5

2

The solution is to get the name of the object's property. You would like to take the property of width of the object portsTool. So you can just use:

portsTool.width

In order to expand the object to see how you will call it you can do this:

for (x in myObj) {
    console.log(myObj[x]);
}
Leonidas
  • 526
  • 11
  • 20
  • Please check this answer too. http://stackoverflow.com/questions/7306669/how-to-get-all-properties-values-of-a-javascript-object-without-knowing-the-key – Leonidas Sep 08 '15 at 13:18
  • console.log(border.width); is printing undefined – kittu Sep 08 '15 at 13:19
  • Your object is called border? – Leonidas Sep 08 '15 at 13:19
  • @Leonidas - probably not :p – Jaromanda X Sep 08 '15 at 13:20
  • variable name is border – kittu Sep 08 '15 at 13:30
  • You should console log all te object with the x inset. Can you paste some more code? Lets say, how does this object is produced? – Leonidas Sep 08 '15 at 13:32
  • `for (x in border) { console.log(border[x]); }` // prints so many objects and when I write `console.log(border[x].width);` //its printing `undefined` then `150` and then `undefined`. so I get the width which is 150 but why is it printing undefined? – kittu Sep 08 '15 at 13:34
  • It is so difficult to answer this question if I cannot see the code. Maybe you declare the border object somewhere else too. Maybe it confuses the border object with the border attribute of javascript. – Leonidas Sep 08 '15 at 13:36
  • Updated the question. – kittu Sep 08 '15 at 13:38
1

"width" is property of the object, wich can be accessed like this: variable.width

Alexandr Lazarev
  • 12,554
  • 4
  • 38
  • 47
1

Assuming that your object is stored in myObj, you can access the property with either

myObj.width

or

myObj["width"]
user1438038
  • 5,821
  • 6
  • 60
  • 94
0

Width is a property of the object, so you would just reference it with its variable named followed by .width.

Mike Feltman
  • 5,160
  • 1
  • 17
  • 38
  • console.log(border.width); is printing `undefined` – kittu Sep 08 '15 at 13:18
  • border is the variable name – kittu Sep 08 '15 at 13:18
  • 1
    First, I'd recommend changing the variable name. Border is not a reserved word in JavaScript, but since it's a common HTML attribute, using is as a variable name is a likely source of confusion. Next, when you expand the variable in the debugger do you actually see a width property? Width is not a property of every HTML element. – Mike Feltman Sep 08 '15 at 13:27
0

Your variable called border is a JointJS element, correct? If so, you can get its width and height like this:

var theWidth = border.attributes.size.width;
var theHeight = border.attributes.size.height;
Mike Goodwin
  • 8,810
  • 2
  • 35
  • 50