Solved

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

Posted on 2011-09-27
12
308 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
while loop in html mail format 5 32
SVG Star 4 16
Angular - filter - one works but other doesn't 13 24
Printing a Google Form 2 8
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
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)

706 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

19 Experts available now in Live!

Get 1:1 Help Now