0

I'm trying to run a function when my page id is true, which is working well and good, but I'm not able to get the values of the function.

What I wanted to achieve is I wanted to load the image URL from the calendar data, into the page which id is true for example if (page1) is true then I wanted to do a query search of image id in that page and load the image from the calendar data.

The getImageUrl(events[0].img); gives me the image URL correctly but when I run this function inside the if (page.id === "page1") {getImageUrl()} the image URL is undefined. Is there a way or a different workaround for this? Is this possible to do? Please advise me. I'm quite new to the development.

document.addEventListener("init", function (event) {
let page = event.target; 
     
     if (page.id === "page1") {

        // if page id is true this function will execute but im not able to get the image says undefined how do i get the function working right.
        getImageUrl();
 
} else if (page.id === "page2") {
          //do something
      }
});

//The below Queryselector id is avialble only for PAGE 1 where i want to run this function. 
function getImageUrl(url) {
  document.querySelector("#image-div").innerHTML += `<img src="${url}" />`;
}


//LOAD Calender and passing json ImageURLdata into the getImageUrl function
function loadCalender() {
  $("#container").simpleCalendar({
    onCalendarLoad : function (date, events) {
     getImageUrl(events[0].img);
     console.log(events[0].img);
    }
}

3
  • 3
    You do not pass an image..... getImageUrl();<--- where is the image src? Commented Dec 10, 2020 at 14:34
  • 1
    The function is named getImageUrl - you mentioned "getting the image", yet the function "SETS" the HTML in the element. So what exactly are you trying to do? Commented Dec 10, 2020 at 14:35
  • Is there a better way to getImageUrl() value inside the page.id if loop, please help me Commented Dec 10, 2020 at 14:47

2 Answers 2

1

I am not quite sure what you want. But I believe this is what you are trying.

document.addEventListener("init", function (event) {
    let page = event.target; 

    var setImage = function (url) {
        var imgDiv = document.getElementById("image-div");
        var img = document.createElement('img');
        img.src = url;
        imgDiv.appendChild(img);
    }

    $("#container").simpleCalendar({
        onCalendarLoad : function (date, events) {
            if (page.id === 'page1') {
                setImage(events[0].img);
            } else if (page.id === 'page2') {
                // do something else
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you mohamed, this is one way to look at it, still few bugs, im sure i can sort them. thank you so much
No problem :) 👍
1

I don't really get what you try to do here.

  1. the getImageUrl(url) function is more a setImageUrl... you are setting a value
  2. the getImageUrl does not return any value so again something which does not make sense at all please provide a better code

1 Comment

Hello sorry, maybe the way i code is wrong, but what i wanted to do is I wanted to get the image URL from the onCalendarLoad : function (date, events) { } to the above if (page.id === "page1") {} how can i acheive this, do i have to store the function return value i a global varibale and will i be able to fetch from there. Please help me.

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.