Get element by title in jQuery

There are some cases where I need to set the value of a field based on title. I try to do this in jQuery in a simple example but it doesn't work.

I have this field:
<input type="text" title="EmployeeMail" name="mail" value="">

Open in new window


I would like to set the value using jQuery:
$(document).ready(function(){
	$"[title='EmployeeMail'].val("name@mail.com");
 }); 

Open in new window


What is the problem?

Kind regards,
Kasper
kasperEHAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Meir RivkinFull stack Software EngineerCommented:
try:
$("input[title='EmployeeMail']").attr('title', 'name@mail.com');

Open in new window

0
Pierre CorneliusCommented:
Instead of title, use id

e.g. <input type="text" id="EmployeeMail" name="mail" value="">

and access like this
 $('#EmployeeMail').val('SomeValue');



Your method should work also but you forgot to enclose in ( ) after $
0
kasperEHAuthor Commented:
sedgwick, I can't get your code to work. See full example below.
PierreC, it works with ID, but I would like to use Title because I want to use this in Sharepoint where the ID may change in some cases (and is very long) but the Title is constant (and is simply the name of the field).

Here is the full html with sedgwick's suggestion:
<html>
<head>
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"> 
</script>
</head>
<body>
<script>
$(document).ready(function(){
	 $("input[title='EmployeeMail']").attr('title', 'name@mail.com');
 }); 
</script> 
Test <b>tekst</b>
<p id="demo">This is a paragraph.</p>

<button type="button" id="test99" class="huttelihut" onclick="displayDate()">Test</button>

<h3>Send e-mail to someone:</h3>

<form action="MAILTO:kaspertm@gmail.com" method="post" enctype="text/plain">
Name:<br>
<input type="text" name="name" value="your name"><br>
E-mail:<br>
<input type="text" title="EmployeeMail" id= "EmployeeMail" name="mail" value=""><br>
Date:<br>
<input type="text" id="date" name="comment" value="your comment" size="50"><br><br>
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>


</body>
</html>

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

GaryCommented:
$("[title='EmployeeMail']").val("name@mail.com");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Meir RivkinFull stack Software EngineerCommented:
try this:
<html>
<head>
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"> 
</script>
</head>
<body>
<script>
$(document).ready(function(){
	 $("input[title='EmployeeMail']").val("name@mail.com");
 }); 
</script> 
Test <b>tekst</b>
<p id="demo">This is a paragraph.</p>

<button type="button" id="test99" class="huttelihut" onclick="displayDate()">Test</button>

<h3>Send e-mail to someone:</h3>

<form action="MAILTO:kaspertm@gmail.com" method="post" enctype="text/plain">
Name:<br>
<input type="text" name="name" value="your name"><br>
E-mail:<br>
<input type="text" title="EmployeeMail" id= "EmployeeMail" name="mail" value=""><br>
Date:<br>
<input type="text" id="date" name="comment" value="your comment" size="50"><br><br>
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>


</body>
</html>

Open in new window

0
Pierre CorneliusCommented:
this
$("input[title='EmployeeMail']").attr('title', 'name@mail.com');
will change the title attribute

it should be

$("input[title='EmployeeMail']").val('name@mail.com');
0
kasperEHAuthor Commented:
Thank you, it works now
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.