jQuery, comparing 2 strings

Hello Folks,
I am capturing SELECT element and Textbox values and I am trying to comparing it to check if they are equal like this

if($("#drpLists").val() == $("#txtListname").val()){

is this not correct?
please advice
Who is Participating?
HI guy,
please try the following and give me your result:
1. Check your #hiddflag tag, do it have a "name" attribute, if not, please add it in.
2. Set the value for #hiddflag in the HTML code, this action is to separate your validation code and your form serialize code, alert the frmParams befor $.ajax, and tell me do the hiddflag appear there? if not means you serialize code have problem.
3. Make sure the id of #hiddflag is "hiddflag", not hiddenflag or something else, this is a very very minor mistake but very very often.
4. Try to add alert before each if statement, to see the value of each .val(), and in the bracket of the if, to see if the jump of statement is correct, the usually mistake here is && and ||, "less than" and "greater than"

Back to me after you try 4 things above :-)
<select> elements contain <option> elements.

<input> elements contain text, usually.

You get the value of a selected <option> element differently than the value of an <input> element.

If jQuery understands this distinction, then your code should work. Otherwise, it won't.
Hi, I will needs your full source code to know what were wrong, your above code is okay, nothing wrong, I have the sample code, use it if you wish, or you can re-attach your code, I will analyze the problem for you.
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js"></script>
<script type="text/javascript">
function compare()
  if ($("#drpLists").val() == $("#txtInput").val()){
    alert("Value equals");
    alert("Value not equals");
<select id="drpLists">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<input type="text" id="txtInput" value="3"/>
<input type="button" onclick="compare()" value="Compare" />

Open in new window

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

newbie27Author Commented:
Hello Guys,
Thanks for your comments, Please see my code attached hopefully you may get to see what I am trying to do here.
Just to give you an overview I am calling addToList() function , wanted to do some validations and based on them passing appropriate flag values ($("#hiddflag").val('2')) to the proxy page to process by calling submitForm() function.

but it is not passing appropriate hidden values in the ajax call.

Thanks for looking.


function addToList()
  if($("#drpLists").val() == 0){
    alert('Please select a list');  
  }else if($("#drpLists").val() == "new" && $("#txtListName").val() == ""){
    alert('Please enter List Name');
if($("#drpLists").val() == "new" && $("#txtListName").val() != ""){
}else if($("#drpLists").val() != "new" && $("#txtListName").val() == ""){
	}else if($("#drpLists").val() == $("#txtListName").val()){
    var result = "";             
    $(".chkRefNos").each( function () 
        if(this.checked == true) {     
             result += "," + $(this).val()  ;
        result = result.substr(1);
            if( result ){
					  submitForm('frmList', 'savefields');
            } else{
				submitForm('frmList', 'savefields'); 	
 //return false;   
 function submitForm(frm, ax){
// requires jquery 
        var myForm = $("#" + frm);
                var param="";
                $(".edit_area").each( function(){param+= "," + escape(this.innerHTML) });
                param = "itemNotes=" + param.substring(1);
        var frmParams = myForm.serialize() + "&" + param;
                 type: "POST",
                url: "list_save_proxy.asp", 
                dataType: "xml",
                data: frmParams,
                success: function(xml){
                                var errCode = $("errorcode", xml).text();
                                var errMsg = $("errormsg", xml).text();
                                var resTxt = $("responsetext", xml).text();
                                if(errCode == 0){
											if(resTxt =="exist"){
											  if(confirm('List Name Already exist, Do you want to over write it?')){
												   submitForm('frmList', 'savefields');  
                                        $('#responsemsg').html('\n<br> > ' + resTxt + '');
                                        $('#responsemsg').append(' [' + errCode + ' ' +  errMsg + ']'); 

Open in new window

newbie27Author Commented:
Yeah you are right I actually have not set the hiddFlag value in the HTML correctly but it is now seems to be working ....!!

However, I am now facing problems in displaying correct data in the Flexigrid. All I am doing there is to pick the name from <SELECT> and when I click on View list button I am doing an Ajax call to show details of selected option in the Grid.
But it is not doing it right.
Can you please have a look it here


thanks for your help
newbie27Author Commented:
No worries - glad to help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.