Link to home
Start Free TrialLog in
Avatar of kasperEH
kasperEHFlag for Denmark

asked on

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
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

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

Open in new window

SOLUTION
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kasperEH

ASKER

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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this
$("input[title='EmployeeMail']").attr('title', 'name@mail.com');
will change the title attribute

it should be

$("input[title='EmployeeMail']").val('name@mail.com');
Thank you, it works now