0

I wrote the below code for changing the text of div that called active yes, based on value of each input with type hidden. i want to change the text of this div if input with id enable to "Enable List" and if input with classname delete has value changes the div text to "Deleted list" and if both of them was null show "list". my code does not work correctly. what is my problem? here is my snippet :

 $(document).ready(function() {
   tmpval = $('#enable').val();
   if (tmpval == '') {
     $('.activeyes').text('list');
   } else {
     $('.activeyes').text('Enable List');
   }  
      
   tmpval2 = $('#delete').val();
   if (tmpval2 == '') {
     $('.activeyes').text('List');
   } else {
	 $('.activeyes').text('Deleted List');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<input type="text" value="aa" id="enable" />
<input type="text" value="" id="delete" />
<h1 class="activeyes"> List</h1>

5
  • 1
    you need to do on change or keyup event instead of on DOM ready Commented Sep 25, 2017 at 6:01
  • @Se0ng11 it is ok . i take the value of my inputs from my addressbar , my problem is another thing Commented Sep 25, 2017 at 6:04
  • 1
    I'm not understanding what you are trying to do. Can you explain in different words? Commented Sep 25, 2017 at 6:04
  • @IzzyCooper i want to change div's text that called activeyes. if input with id enable was not null set text to enable list, and if input with id delete was not null set that text to deleted list , anf if both of these inputs were null change text to list Commented Sep 25, 2017 at 6:06
  • Still not getting it. Are the inputs [id="enable"] and [id="delete"] meant to be textboxes? Shouldn't they be checkboxes that can be toggled, or am I missing something? Commented Sep 25, 2017 at 6:17

2 Answers 2

3

You are overwriting effect of the first check by the second check; you need to check the 2 inputs value together. Still, it is unclear what will happen if both are non-empty.

$(document).ready(function() {
  tmpval = $('#enable').val();
  tmpval2 = $('#delete').val();
  if (tmpval == '' && tmpval2 == '') {
    $('.activeyes').text('list');
  } else if( tmpval!='' ){
    $('.activeyes').text('Enable List');
  } else if( tmpval2!='' ){
    $('.activeyes').text('Deleted List');
  }  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<input type="text" value="aa" id="enable" />
<input type="text" value="" id="delete" />
<h1 class="activeyes"> List</h1>

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

1 Comment

That will not happen and i have only one of them with value. Thank You in Advance
1

what is my problem?

You need to check the value of input when it changes its value, so capture the change event.

$(document).ready(function() {

  $('#enable, #delete').change(function() {
    var $this = $(this);
    var id = $this.attr("id");
    var value = $this.val();
    if (value.length == 0) 
    {
      $('.activeyes').text('list');
    } 
    else 
    {
      id == "enable" ? $('.activeyes').text('Enable List') : $('.activeyes').text('Deleted List');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<input type="text" value="aa" id="enable" />
<input type="text" value="" id="delete" />
<h1 class="activeyes"> List</h1>

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.