Solved

jQuery, comparing 2 strings

Posted on 2008-06-13
7
8,682 Views
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()){
      $("#hiddflag").val('2');      
}

is this not correct?
please advice
thanks
s
      
0
Comment
Question by:newbie27
  • 3
  • 2
  • 2
7 Comments
 
LVL 29

Assisted Solution

by:Badotz
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.
0
 
LVL 3

Expert Comment

by:KhoiNqq
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.
<html>
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js"></script>
<script type="text/javascript">
function compare()
{
  alert($("#drpLists").val());
  alert($("#txtInput").val());
  if ($("#drpLists").val() == $("#txtInput").val()){
    alert("Value equals");
  }else{
    alert("Value not equals");
  }
}
</script>
</head>
 
<body>
<select id="drpLists">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="text" id="txtInput" value="3"/>
<input type="button" onclick="compare()" value="Compare" />
</body>
</html>

Open in new window

0
 
LVL 8

Author Comment

by:newbie27
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.

Regards
Sam

function addToList()
{
  if($("#drpLists").val() == 0){
    alert('Please select a list');  
  }else if($("#drpLists").val() == "new" && $("#txtListName").val() == ""){
    alert('Please enter List Name');
  }else{
	
if($("#drpLists").val() == "new" && $("#txtListName").val() != ""){
		$("#hiddflag").val('0');	
}else if($("#drpLists").val() != "new" && $("#txtListName").val() == ""){
		$("#hiddflag").val('1');	
	}else if($("#drpLists").val() == $("#txtListName").val()){
		$("#hiddflag").val('2');	
	}
	
    var result = "";             
    $(".chkRefNos").each( function () 
        {
        if(this.checked == true) {     
             result += "," + $(this).val()  ;
        }
        });
        result = result.substr(1);
            if( result ){
				
					 					  					 
					  submitForm('frmList', 'savefields');
					
            } else{
				checkAll("on");
				submitForm('frmList', 'savefields'); 	
			} 
  }
 //return false;   
} 
  
 
 function submitForm(frm, ax){
// requires jquery 
        $('#action').val(ax);
        var myForm = $("#" + frm);
                var param="";
                $(".edit_area").each( function(){param+= "," + escape(this.innerHTML) });
                param = "itemNotes=" + param.substring(1);
 
        var frmParams = myForm.serialize() + "&" + param;
         
                $.ajax({
                 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');  
												   $("#hiddflag").val('4');
												}
											 }else{                                
												$('#responsemsg').html(resTxt);
												repopulateList();
											}	
								}else{
                                        $('#responsemsg').html('\n<br> > ' + resTxt + '');
                                        $('#responsemsg').append(' [' + errCode + ' ' +  errMsg + ']'); 
                                }
                            }  
        });
        }

Open in new window

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 3

Accepted Solution

by:
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 :-)
0
 
LVL 8

Author Comment

by:newbie27
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

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23471788.html

thanks for your help
sam
0
 
LVL 8

Author Closing Comment

by:newbie27
ID: 31466983
thanks
0
 
LVL 29

Expert Comment

by:Badotz
ID: 21782789
No worries - glad to help.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

786 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