Solved

Get id of element - jquery

Posted on 2014-09-30
4
124 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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

688 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