Solved

Get id of element - jquery

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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)

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now