0

I want o use function with same name in js, basically there is one link:

<a onClick="showjury1();" >show</a>

when the user is on desktop I want this code to be executed:

if (window.screen.width >= 1150) {
  function showjury1() {
    document.getElementById("jury1").style.display = "block";
    document.getElementById("juryline1").style.display = "none";
  }
}

and when the user is on mobile or at any other resolution I want this code to be executed:

if (window.screen.width <= 500) {
  function showjury1() {
    document.getElementById("jury1").style.display = "none";
    document.getElementById("juryline1").style.display = "block";
  }
}

Is this possible? I have executed it and it gives errors; jury1 is not defined etc.

3
  • What are the exact errors? Commented Apr 4, 2016 at 6:52
  • why not add the screen width condition inside the function body?that way you can have single function Commented Apr 4, 2016 at 6:53
  • it gives errors like showjury1, jury1, juryline1 are not defined. Commented Apr 4, 2016 at 6:53

4 Answers 4

2

Why do you need to define that function twice? Just use your conditional statement correctly

function showjury1()
{
    if(window.screen.width >= 1150)
    {
         document.getElementById("jury1").style.display = "block";
         document.getElementById("juryline1").style.display = "none";
    }
    else if(window.screen.width <= 500)
    {
        document.getElementById("jury1").style.display = "none";
        document.getElementById("juryline1").style.display = "block";
    }
    else
    {
       // might want to do something here too
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

it is working on desktop but when i press ctrl+shft+m it gives errors which means it is not executing the code after else if(window.screen.width <= 500)
It wont magically execute again if you change screen size. You are calling it only on button click. So after changing the screen size you might want to click the link again
did you click the button after changing the window size?
1

You can also do something like:

if(window.screen.width >= 1150){
  window.showjury1 = function(){
    ...
  }
}else{
  window.showjury1 = function(){
    ...
  }
}    

Comments

0

You can put the condition into the function, then you don't need to define the same function twice, because that isn't possible the way you want it to be.

function showjury1 {
if(window.screen.width >= 1150)
{
     document.getElementById("jury1").style.display = "block";
     document.getElementById("juryline1").style.display = "none";
}
else if(window.screen.width <= 500)
{
    document.getElementById("jury1").style.display = "none";
    document.getElementById("juryline1").style.display = "block";`
}
}

Comments

0

Instead of defining your showjury1 function inside your if statements, put your if statements inside your showjury1 function.

showjury1(){
    if(check window width){...}
    else if(check different width){....}
}

and make sure your html is loaded before your Javascript otherwise you will get a undefined error when you try to click your button that calls your function.

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.