Solved

Get id of element - jquery

Posted on 2014-09-30
4
119 Views
Last Modified: 2014-09-30
Hello experts.
I need help to get the id of one input text field by clicking on a span within a label before the field but i can't get it work. Any help?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<label>Youtube address <span class="checkyoutubelink">Test</span></label>
<input name="youtubelink" id="youtubelink" class="youtubelink" type="text"value="">
</body>
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
 <script type="text/javascript">
 $('span.checkyoutubelink').click(function(){
	  var tempid = $(this).closest().find('input[type=text]').attr('id');
	  alert(tempid);  
});
 </script>
</html>

Open in new window

0
Comment
Question by:Panos
[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
  • 2
  • 2
4 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40352721
test page : http://jsfiddle.net/42qy2m3z/

$('span.checkyoutubelink').click(function(){
	  var tempid = $(this).closest("label").next('input[type=text]').attr('id');
	  alert(tempid);  
});

Open in new window

0
 
LVL 2

Author Comment

by:Panos
ID: 40352727
Hi leakim971
Great! Thank you very much it works
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 40352728
Thank you again
regards
panos
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40352744
'welcome
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 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