Solved

how to get the value of a specific field using .attr or other in jquery

Posted on 2011-09-27
12
311 Views
Last Modified: 2012-05-12
HI Im trying to get the <input type name="flag" value using jquery

I thought of using .attr function on the form element but I cant figure out how to get that

(Im doing it becuase I need to check if the form is sumbitted )
<form id = "popup_form_{$row['id']}" name= "nurit" action ="" method ="post" style="background-color:white; border:2px solid blue;padding:4px; width:250px; display :none; position:absolute"  onsubmit = "return false;">

			<input type="hidden" name="id" value ="{$row['id']}">

			<input type="hidden" name ="email" value ="{$row['email_address']}">
			<input type ="hidden" name ="flag" value = "test" >

			<input type="submit" value="SpamIt" name="spamIt" style="margin-right:15px;">

			<input type="button" value="Close" onclick="closePopup()"/>

			

			</form>

Open in new window

0
Comment
Question by:Nura111
  • 6
  • 5
12 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 167 total points
ID: 36713413
Give your element an id like:
<input type ="hidden" id="flag" value="test" >

Open in new window


Get the value like:

var x = $("#flag").val();

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 333 total points
ID: 36713415
you may use :
var value = $(":hidden[name='flag']").val();
alert(value);

// OR

// your way (using attr)
value = $(":hidden[name='flag']").attr("value");
alert(value);

Open in new window


test page : http://jsfiddle.net/EvrnJ/
0
 

Author Comment

by:Nura111
ID: 36713438
and also how can I set the value of this element to something else please?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36713450
var something = 10;

$(":hidden[name='flag']").val( something  );

or

$(":hidden[name='flag']").attr("value", something);
0
 

Author Comment

by:Nura111
ID: 36713569
Hi the problem again is that I need something more uniqueI have more than one popup_form_{$row['id']}"
in the page how can I conecct to the id number of the form..
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 333 total points
ID: 36713582
var something = 10;
var popup_form_id = "popup_form_{$row['id']}";

$(":hidden[name='flag']", "#"+popup_form_id).val( something  );
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Nura111
ID: 36713618
no its not working isnt the comma is adding the elements so it will chose 2 Dom element in that case?
0
 

Author Comment

by:Nura111
ID: 36713628
sorry it is working my mistake but still isnt comma is adding the elements so it will chose 2 Dom element in that case?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36713646
$("one,two")

is not

$("one" , "two")

if you prefer use the following :

$("#"+popup_form_id).find(":hidden[name='flag']").val( something  );
0
 

Author Comment

by:Nura111
ID: 36713756
Thank you leakim971: Can we go back to the issue form yesterday
 I can post it here or there

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


the function that was handeling the click and that sumbiiting the form
function toggleLeadBody(id) {													
					var fieldName1 = "#spam_"+ id + "_body";
					var fieldName2 = "#sr_" + id + "_body";
					var popup_form_id = "popup_form_" + id;
					 
					var element = $(fieldName2);
                    element.toggle();   
					
					var fElement = $(fieldName1);
				//checking to see if its not been marked as spam	
				if ($(":hidden[name='flag']","#"+popup_form_id).val() =="no"){
					 fElement.toggle();	
					}
    //sumbitting the form			   	    
					fElement.submit(function(e) {
                            alert("Hello world!");
							//fElement.attr('flag','1');
							e.preventDefault();
						//"mark" the input name =flag as spam 
						    $(":hidden[name='flag']","#"+popup_form_id).val("on");
							fElement.toggle();
							
					})                 
					

            }

Open in new window



I attached the form as well

So my only problem is that I want to update the db without changing anything in the page I think I need to use ajax but I dont know hoe to do it without loading anything to the element
<form id = "popup_form_{$row['id']}"  action ="" method ="post" style="background-color:white; border:2px solid blue;padding:4px; width:250px; display :none; position:absolute"  onsubmit = "return false;">

			<input type ="hidden" name="flag" value = "no" >

			<input type="hidden" name="id" value ="{$row['id']}">

			<input type="hidden" name ="email" value ="{$row['email_address']}">

			<input type="submit" value="SpamIt" name="spamIt" style="margin-right:15px;">

			<input type="button" value="Close" onclick="closePopup()"/>

			

			</form>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 36713923
if THIS question is answered you MUST close it.

you may request attention for your other thread : I'm not alone here and not the best.
0
 

Author Comment

by:Nura111
ID: 36713932
ok I just thought you might be intrested you spend quite a while there at that question and I think it will be hard to follow for somebody else
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

15 Experts available now in Live!

Get 1:1 Help Now