1

I want to make a common function that can be call in html so I can update the value from variable.

function getActivityPoint(activityName) {
   document.getElementsByClassName('share_points')[0].innerHTML = activityName;
}

my variable

var ap = {
   'movie_review':'20',
}

And in HTML I would like to do this.

<div class="share_points" onload="getActivityPoint(ap.movie_review)">-</div>
<div class="share_points" onload="getActivityPoint(ap.game_review)">-</div>
.............
2
  • 2
    So...what's the problem? Commented Apr 12, 2018 at 19:16
  • its not working Commented Apr 12, 2018 at 19:21

2 Answers 2

2

The inline onload event does not work for a div.

You may change your code in order to change your divs, using data attributes, like:

<div class="share_points" data-load="movie_review">-</div>

and on window load you can do your work:

window.addEventListener("load", function(event) {
    document.querySelectorAll('.share_points').forEach(ele => getActivityPoint(ele, ap[ele.dataset.load]));
});
function getActivityPoint(ele, activityName) {
    ele.textContent = activityName;
}

var ap = {
    'movie_review':'20',
    'game_review': 10
}

/****************
 For Compatibility issue you may write:

window.addEventListener("load", function (event) {
            Array.prototype.slice.call(document.querySelectorAll('.share_points')).forEach(function (ele) {
      getActivityPoint(ele, ap[ele.dataset.load]);
   });
});

For IE < 9 the solution is:

 window.addEventListener("load", function (event) {
  var divs = document.querySelectorAll('.share_points');
  for (i = 0; i < divs.length; i++) {
      getActivityPoint(divs[i], ap[divs[i].dataset.load]);
  }
});
        
*/
<div class="share_points" data-load="movie_review">-</div>
<div class="share_points" data-load="game_review">-</div>

Sign up to request clarification or add additional context in comments.

6 Comments

Are all browsers equal?
@Xotic750 no not at all but I am just trying to add the value from variable to html element. and it should be there at for all browsers.
@Xotic750 No not all. I don't care that below 9.
I personally don't even care for IE11 support, but IE<9 doesn't have support for addEventListener, IE<8 doesn't support querySelectorAll and dataset is not supported below IE11 :)
Guys Please suggest which one should I use ? 1st, 2nd or 3rd solution ? dataset not working in ie9 and ie10 ? should I use the first one ?
|
1

You can do:

var ap = {
      'movie_review': '20',
      'game_review': '100'
    },
    share_points = document.getElementsByClassName('share_points');

window.addEventListener('load', function() {
  Array.prototype.forEach.call(share_points, function(el) {
    el.innerHTML = ap[el.dataset.review];
  });
});
<div class="share_points" data-review="movie_review">-</div>
<div class="share_points" data-review="game_review">-</div>

4 Comments

so for next I have to do ` share_points[1].innerHTML = ap.game_review;` ?
Yes.. I am improving my answer to make it more dynamic.. @gaetanoM is a better answer
Improved answer
Thanks for your effort but I guess this code will not run over all browsers. That arrow => might be buggy. don't you think ?

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.