Solved

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

Posted on 2011-09-27
12
315 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
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 …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

732 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