0

So, here is my HTML code and jQuery code. I want to pass the value attribute to jQuery so that I can use it as an parameter for my if else loop but I am unable to do so. Here is my sample code.

HTML Code:

<select id="counting" class="normal_button">
    <option value="" disabled="disabled" selected="selected">value  type</option>
    <option value="1">one</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
   <option value="4">Four</option>  
</select>

jQuery Code:

jQuery(document).ready(function(){
    jQuery("#counting").click(function(){
        if (value == "1") 
        {
            jQuery("#text_area").append("<p>Test</p>");
        }
        else {
            jQuery("#text_area").append("<p>Tests</p>");        
        }
    });
});

So here I want that if the options change then the text appended also changes hence I have used value as my parameter for if condition. Also I am appending text to another div text area, is that fine? Can I do that?

I am new to this so please help me. It's showing me the error value is not defined.

3 Answers 3

1

Is this what you wanted?

jQuery(document).ready(function(){
     jQuery("#counting").bind("change", function(){
          if ($(this).val() == "1") 
          {
               jQuery("#text_area").append("<p>Test</p>");
          }
          else {
               jQuery("#text_area").append("<p>Tests</p>");        
          }
     }); 
 });

I just made 2 changes -

1) used bind("change"), instead of click and

2) used $(this).val() instead of value.

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

9 Comments

i cant see any error but i also can't see the appended item
check if you have a div or something similar with id = text_area. Here's a fiddle that shows it working Cheers! Note that you have to change the select box to make it do something.
what do you mean by change the select box
select a value in the select box to trigger the change event.
like can you pleas elaborate i am new to this
|
0

when you have select boxes and you want something happen on selecting that selectbox, you can not use .click function. .click function is for when something is clicked. but here you select. so first of all you should use .change function.

and when you want to have value of selected select box, value variable is not exist by itself. you should use $(this).val().

$this is your select box which you select something from it,and $(this).val() returning the selected value.

here is a working plunker

jQuery(document).ready(function(){
   jQuery("#counting").change(function(){
      if ($(this).val() == "1") 
      {
          jQuery("#text_area").append("<p>Test</p>");
      }
      else {
          jQuery("#text_area").append("<p>Tests</p>");        
      }
   });
  });

8 Comments

i am not able to view text output in the text area
@kamehamehaaaaaa where is your text area? in plunker I created there is a text area and you can see text output there
@kamehamehaaaaaa do you have any div in your HTML with id="text_area" ? cause you should have one ;)
ya i have a div which is basically named text_area where i see the results
@kamehamehaaaaaa no, your div should have id="text_area" not named text_area
|
0

jQuery(this).val() would give the value of selected option. This needs to be binded on selection change event.

jQuery(document).ready(function() {
  jQuery('#counting').change(function() {
    value = jQuery(this).val();
    if (value == "1") {
      jQuery("#text_area").append("<p>Test</p>");
    } else {
      jQuery("#text_area").append("<p>Tests</p>");
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="counting" class="normal_button">
  <option value="" disabled="disabled" selected="selected">value type</option>
  <option value="1">one</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
  <option value="4">Four</option>
</select>
<div id="text_area">

</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.