3

I made input buttons, and for some reason they are not triggering my functions ... can some one shine a light on this?

<div id="windowBar">
  <h3>If you were Chuck Norris...</h3>
  <input id="WindowClose" type="button" onclick="close();"></input>
  <input id="windowSmall" type="button" onclick="min();"></input>
</div>

and my jQuery:

function close() {
  $("#inputWindow").hide(300);
}

function min() {
  if (minimize === false) {
    $("#inputWindow").hide();
    $('#windowBar2').css('display','block');
    $('#windowBar2 h3').css('display','block');
    minimize = true;
  } else {
    $("#inputWindow").show();
    $('#windowBar2').css('display','none');
    $('#windowBar2 h3').css('display','none');
    minimize = false;
  }
}
13
  • 4
    put them inside document.ready Commented Dec 21, 2015 at 10:20
  • 1
    @guradio, the functions are called using onclick. So, both functions and the DOM element are defined in DOM tree wen executing the code. If he puts the functions in document.ready, then they won't be defined in the global scope. The best practice is to define functions out of $() scope. Commented Dec 21, 2015 at 10:23
  • 1
    Where does minimize come from? Commented Dec 21, 2015 at 10:23
  • 1
    Are you getting any errors in the console? Commented Dec 21, 2015 at 10:24
  • it is declared on top of file.. here is a pen: codepen.io/shiva112/pen/dGMrWB Commented Dec 21, 2015 at 10:24

5 Answers 5

3

Try changing the close function name to something else like closeTag because it may be confusing it with the window.close() function. And after changing the function name it's working now please check -

function closeTag() {
  $("#inputWindow").hide();
}

function minTag() {
  if (minimize === false) {
    $("#inputWindow").hide();
    $('#windowBar2').css('display','block');
    $('#windowBar2 h3').css('display','block');
    minimize = true;
  } else {
    $("#inputWindow").show();
    $('#windowBar2').css('display','none');
    $('#windowBar2 h3').css('display','none');
    minimize = false;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="windowBar">
  <h3>If you were Chuck Norris...</h3>
  <input id="WindowClose" type="button" value="Close" onclick="closeTag()"/>
  <input id="windowSmall" type="button" value="Min" onclick="minTag()"/>
  
  <div id="inputWindow">
  hide text on Close button click
  </div>
</div>

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

3 Comments

Can you check my answer
@Jagadeesh in your case if I want to call any function from input then I can't because you fixed the function with ID that I think user don't want.
@ShivaTraanman adding minimize = true; to close function will work for you
2

Your JavaScript functions need to be defined above your HTML fragment. Like this:

<script>
function close() {
  $("#inputWindow").hide(300);
}

function min() {
  if (minimize === false) {
    $("#inputWindow").hide();
    $('#windowBar2').css('display','block');
    $('#windowBar2 h3').css('display','block');
    minimize = true;
  } else {
    $("#inputWindow").show();
    $('#windowBar2').css('display','none');
    $('#windowBar2 h3').css('display','none');
    minimize = false;
  }
}
</script>
<div id="windowBar">
  <h3>If you were Chuck Norris...</h3>
  <input id="WindowClose" type="button" onclick="close();"></input>
  <input id="windowSmall" type="button" onclick="min();"></input>
</div>

Or you can do it the jQuery way:

<script>
document.ready(function() {

    $('#WindowClose').click( function() {
      $("#inputWindow").hide(300);
    });

    $('#windowSmall').click( function() {
      if (minimize === false) {
        $("#inputWindow").hide();
        $('#windowBar2').css('display','block');
        $('#windowBar2 h3').css('display','block');
        minimize = true;
      } else {
        $("#inputWindow").show();
        $('#windowBar2').css('display','none');
        $('#windowBar2 h3').css('display','none');
        minimize = false;
      }
    });
});
</script>
<div id="windowBar">
  <h3>If you were Chuck Norris...</h3>
  <input id="WindowClose" type="button"></input>
  <input id="windowSmall" type="button"></input>
</div>

5 Comments

can you fork my pen, i cant seem to get this working either... codepen.io/shiva112/pen/dGMrWB
I have given working snippet. can you check is it your requirement? if yes mark it as answer
I guess your requirement is when click on close it should hide input. and if click on open it should open the input window and again if we click on open it should hide. am i correct?
@Jagadeesh i dont get you, the requirements are right there in the code. the functions work just fine, i just dont get them to function when i press the button..
@ShivaTraanman make sure add minimize = true; in close function
1

Add javascript:close() in on click instead of directly calling that function close() use like this

onClick="javascript:close()", onClick="javascript:min()".

1 Comment

are you writing javascript in same page itself or seperate if it is seperate please link that file to your code
1

Consider to use return false or event.preventDefault() in your functions.

var minimize;
function close() {
  $("#inputWindow").hide(300);
  return false;
}

function min() {
  if (minimize === false) {
    $("#inputWindow").hide();
    $('#windowBar2').css('display','block');
    $('#windowBar2 h3').css('display','block');
    minimize = true;
  } else {
    $("#inputWindow").show();
    $('#windowBar2').css('display','none');
    $('#windowBar2 h3').css('display','none');
    minimize = false;
  }
  return false;
}

Comments

1

$(function(){

  var minimize = false
  function close() {
    $("#inputWindow").hide(300);
    minimize = true
  }

function toggleInputWindow() {
  if (minimize === false) {
    $("#inputWindow").hide();
    $('#windowBar2').css('display','block');
    $('#windowBar2 h3').css('display','block');
    minimize = true;
  } else {
    $("#inputWindow").show();
    $('#windowBar2').css('display','none');
    $('#windowBar2 h3').css('display','none');
    minimize = false;
  }
}
  $('#WindowClose').on('click',function(){
      close();
  })
  
  $('#windowSmall').on('click',function(){
      toggleInputWindow();
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="windowBar">
  <h3>If you were Chuck Norris...</h3>
  <input id="WindowClose" type="button" value="close"/>
  <input id="windowSmall" type="button" value="open"/>
</div>
<div id="inputWindow">input window</div>

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.