3

I would like to get dynamically the width of the div.progress-bar from the ng-click. My function move($event) on the element div.progress-bar returns the event with the element div.progress-bar. But when I click on div.time which is inside div.progress-bar, it returns the event with the element div.time.

I just want the width of the progress-bar even I click on his children.

HTML :

    <div class="progress-bar" ng-click="move($event)">
        <div class="time" ng-style="{'width': (current_track.time_current / current_track.time_total * 100) + '%'}"></div>
    </div>

CONTROLLER :

$scope.move = function(e) {
    console.log(e);
    console.log(e.srcElement.offsetWidth);
    var widthOfProgressBar = e.srcElement.offsetWidth;
    /* ... */
};

2 Answers 2

2

You have to check if the clicked element is .progress-bar, and if it is not, fetch the parent element:

var bar = e.srcElement.className === 'time'
    ? e.srcElement.parentNode
    : e.srcElement;

var widthOfProgressBar = bar.offsetWidth;
/* ... */

Or alternative more Angular/jqLite way:

var element = angular.element(e.srcElement),
    bar = (element.hasClass('time') ? element.parent() : element)[0];

var widthOfProgressBar = bar.offsetWidth;
/* ... */
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer @VisioN ! I take the first solution ;)
0
$scope.move =  function(e){
    var width = angular.element(e.srcElement)[0].offsetWidth;

    console.log(e.offsetX ,"/", width);
}

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.