kasperEH
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:
I would like to set the value using jQuery:
What is the problem?
Kind regards,
Kasper
I have this field:
<input type="text" title="EmployeeMail" name="mail" value="">
I would like to set the value using jQuery:
$(document).ready(function(){
$"[title='EmployeeMail'].val("name@mail.com");
});
What is the problem?
Kind regards,
Kasper
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
this
$("input[title='EmployeeMa il']").att r('title', 'name@mail.com');
will change the title attribute
it should be
$("input[title='EmployeeMa il']").val ('name@mai l.com');
$("input[title='EmployeeMa
will change the title attribute
it should be
$("input[title='EmployeeMa
ASKER
Thank you, it works now
Open in new window