0

I have a simple javascript that i keep using in every page I have, and I want to make it a function and add some additional feature, if i click that loading_input(div) from display:block change to display:none

I try but I have no luck can someone guide me to make it work..

example:

<script type="text/javascript">
 document.getElementById("instructor").onchange = function() {
    document.getElementById("loading_input").style.display = "block";
    document.getElementById("instructor").style.color = "#fff";
}
document.getElementById("subject_scheduling").onchange = function() {
    document.getElementById("loading_input").style.display = "block";
    document.getElementById("subject_scheduling").style.color = "#fff";
}

</script>
4
  • thanks for your suggestion but I prepare to use javascript Commented Jul 16, 2015 at 7:10
  • What tags are instructor and subject_scheduling ? You are saying "when i click", so maybe you should use .onclick instead of .onchange Commented Jul 16, 2015 at 7:11
  • @jhunlio Jquery is JavaScript, it's a library. Commented Jul 16, 2015 at 7:11
  • @ben instructor is an id of input field where i use to add value before making any event. Commented Jul 16, 2015 at 7:16

3 Answers 3

1

Thank you for all suggestion it help me to come up with this answer.

// To haddle my onchange this is what I come up 
function loading(input_id){
  if(input_id){
    document.getElementById("loading_input").style.display = "block";
    document.getElementById(input_id).style.color = "#fff";
  }
 }

If I need to use it in my page. I just do like this

<input id="sample" onchange="loading('sample')" />

onclick event

 // this is what I come up to haddle my onclick event 
 function close_and_reload(id){
 if(id){
    document.getElementById(id).style.display = "block";
    window.location.reload();
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I made both element Ids parameters to the function, although the code snippet you sent seems like you only change the 2nd one. My version is a bit more flexible but if you know for sure the first one always the same, then you can drop the first parameter.

function changeHandler(elem1, elem2){
    document.getElementById(elem1).style.display = "block";
    document.getElementById(elem2).style.color = "#fff";

} 


document.getElementById("instructor").onchange = changeHandler("loading_input", "instructor");

document.getElementById("subject_scheduling").onchange = changeHandler("loading_input", "subject_scheduling");

Comments

0

Try this.

<!DOCTYPE html>
<html>
<body>

<p>Select a new car from the list.</p>

<select id="mySelect" onchange="changeStyle('demo');changeColor('demo')">
  <option value="Audi">Audi
  <option value="BMW">BMW
  <option value="Mercedes">Mercedes
  <option value="Volvo">Volvo
</select>


<p id="demo">some content</p>

<script>
function changeStyle(id) {
    document.getElementById(id).style.display = "block";
}

function changeColor(id) {
   document.getElementById(id).style.color = "#fff";
}
</script>

</body>
</html>

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.