Get element by title in jQuery

kasperEH
kasperEH used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Meir RivkinFull stack Software Engineer

Commented:
try:
$("input[title='EmployeeMail']").attr('title', 'name@mail.com');

Open in new window

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 $

Author

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

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Expert of the Year 2014
Top Expert 2014
Commented:
$("[title='EmployeeMail']").val("name@mail.com");
Meir RivkinFull stack Software Engineer
Commented:
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

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

it should be

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

Author

Commented:
Thank you, it works now

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial