jQuery, comparing 2 strings

Posted on 2008-06-13
Last Modified: 2010-04-21
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
Question by:newbie27
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 29

Assisted Solution

Badotz earned 100 total points
ID: 21780579
<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.

Expert Comment

ID: 21780975
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=""></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


Author Comment

ID: 21781064
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Accepted Solution

KhoiNqq earned 400 total points
ID: 21781565
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 :-)

Author Comment

ID: 21782409
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

Author Closing Comment

ID: 31466983
LVL 29

Expert Comment

ID: 21782789
No worries - glad to help.

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question