Solved

jQuery, comparing 2 strings

Posted on 2008-06-13
7
8,668 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
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 3

Accepted Solution

by:
KhoiNqq earned 400 total points
Comment Utility
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
Comment Utility
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
Comment Utility
thanks
0
 
LVL 29

Expert Comment

by:Badotz
Comment Utility
No worries - glad to help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now