?
Solved

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

Posted on 2011-09-27
12
Medium Priority
?
317 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
[X]
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
  • 6
  • 5
12 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 668 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 1332 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
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 1332 total points
ID: 36713582
var something = 10;
var popup_form_id = "popup_form_{$row['id']}";

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

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