Calling Field Value in Javascript

Support MB Beaver Builder Integration Calling Field Value in JavascriptResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #36957
    nathan@brilliantly.net[email protected]
    Participant

    I'm trying to call the value in a Metabox custom field (number) into this Javascript I'm using on a page layout in Beaver Builder.

    In "Count1", I want to replace the target number (marked with *) in this code with the value of the metabox field.

    Is there any way to do this?

    document.addEventListener("DOMContentLoaded", () => {
    function counter(id, start, end, duration) {
    let obj = document.getElementById(id),
    current = start,
    range = end - start,
    increment = end > start ? 1 : -1,
    step = Math.abs(Math.floor(duration / range)),
    timer = setInterval(() => {
    current += increment;
    obj.textContent = current;
    if (current == end) {
    clearInterval(timer);
    }
    }, step);
    }
    counter("count1", 50, *25, 2000);
    });

    Thank you very much!

    #36966
    Long NguyenLong Nguyen
    Moderator

    Hi Nathan,

    The best way to use a field value in JavaScript code is to use the WordPress function wp_localize_script(), then you can access the value via JS variable. Please read more on the documentation https://developer.wordpress.org/reference/functions/wp_localize_script/
    and refer to this article https://metabox.io/display-all-listings-on-a-map/?swcfpc=1

    #36991
    nathan@brilliantly.net[email protected]
    Participant

    Thank you for the information.

    UPDATE: Beaver Builder 2.6 has this feature built in to the Number Counter so we will be able to set beginning and ending numbers. And both can be dynamic field data.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.