Yoann Picquenot Yoann Picquenot - 4 months ago 13
Javascript Question

Undefined but existing

Here's my code:

js

var priorities = {
banner: '300',
avatar: '301'
};
var editableDiv = $('*[data-edit]');

editableDiv.append('<div class="overlay-edit"></div>');
editableDiv.each(function(index, value) {
var actualPriorities = value.getAttribute("data-edit");

console.log(actualPriorities);
console.log(priorities[actualPriorities]);
});


html/php

<div class="hexagon-wrap" style="height:<?php echo $this->outterHeight;?>px; width:<?php echo $this->outterWidth;?>px"
<?php
if($this->htmlOptions) {
foreach ($this->htmlOptions as $key => $value) {
echo $key . "=" . $value . "&nbsp";
}
}
?>
>


$this->htmlOptions contains

array('data-edit' => 'avatar')


The output :


banner
300
avatar 
undefined


I do not understand why my last result is undefined. When I'm trying

priorities['avatar']; // ouput is 301


The output is good.

Answer

The problem is the no breaking space character, and you can improve the code wrapping in quotes:

<div class="hexagon-wrap" style="height:<?php echo $this->outterHeight;?>px; width:<?php echo $this->outterWidth;?>px"
<?php
if($this->htmlOptions) {
    foreach ($this->htmlOptions as $key => $value) {
        echo $key . "='" . $value . "' ";
    }
}
?>

Note that I change the &nbsp; with the ' (with a real space to separate the attributes)

Comments