1

I have jQuery code where i am trying to put a class values in a variable & then trying to use that variable value in if else condition & comparing with static value 'FCL' But that's not working.class name is 'kuuj' & this class value can be (LCL,FCL,BULK,AIR) Only.

var xxssees = $('.kuuj').val();
if(xxssees == "FCL")
{
$('#sonal').html(xxssees);	
}
else 
{
   $('#sonal').html('hello');	
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>Solution : </strong><label id="sonal" style="color:Green"></label><br />

4
  • 2
    val() is only applicable for an input element. Can you add the html for class kuuj Commented Mar 10, 2017 at 4:42
  • if you want to call class name , you need to get that class's parent element Commented Mar 10, 2017 at 4:43
  • the issue is the class kuuj value is not passed in to the xxssees variable.i want to pass class value & then need to use in if else condition if variable value is equals to "FCL" bcz i have to use it furture.this all is dynamic & class value calculated after some calculations. but the class values can be only either LCL or FCL or AIR or BULK. so if you can make this working by making adding/changing some code ther then please do this,by using theat class values only. Regards- Ankit Commented Mar 10, 2017 at 4:49
  • @Ankit check answers below. Either your html is wrong or your code have some issue.You will get answer of both below Commented Mar 10, 2017 at 4:53

3 Answers 3

3

1.val() is only applicable for an input element and then your code will work fine.check below:-

$(document).ready(function(){
  var xxssees = $('.kuuj').val();
  if(xxssees == "FCL"){
     $('#sonal').text(xxssees);  
  }else {
     $('#sonal').text('hello');  
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>Solution : </strong><label id="sonal" style="color:Green"></label><br />

<input class="kuuj" value = "FCLs">

2.If kuju class is on any other element apart from input then use .text() or .html() to get the value. Check below:-

$(document).ready(function(){
  var xxssees = $('.kuuj').html();  // you can use .text() also
  if(xxssees == "FCL"){
      $('#sonal').html(xxssees);  //you can use .text() also
  }else {
      $('#sonal').html('hello');  //you can use .text() also
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>Solution : </strong><label id="sonal" style="color:Green"></label><br />

<div class="kuuj">FCLs</div>

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

1 Comment

yes thank you so much sir its working now. thanks alot sir .Regards - Ankit
0

You need to add input field rather then label

Instead of

<label id="sonal" style="color:Green"></label>

You add

 <input type="text" id="sonal" value="" />

feel free to ask if required further help

Comments

0

$(document).ready(function(){
  var xxssees = $('.kuuj').val();
  console.log(xxssees);
  if(xxssees == "FCL"){
     $('#sonal').html(xxssees);  
  }else {
     $('#sonal').html('hello');  
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>Solution : </strong><label id="sonal" style="color:Green"></label><br />

<input type="hidden" class="kuuj" value = "FCL">

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.