Reputation: 51
I have the below code
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").click(function(){
var parts = $('#gitrepo').attr('value');
alert(parts);
return false;
});
});
</script>
</head>
<body>
<p id="gitrepo">http://github.com/username/repo</p>
</body>
</html>
I need extract the value to the parts variable in the script, and split it by '/'. I was able to split it by '/' using .split('/') when i pass a string to parts, like var parts="http://www.github.com",
but when i try to take it from the html using the id tag, the alert shows 'undefined' for the value of 'parts'. How can i extract the value between <p></p>
that has the id"gitrepo"?
I tried $('#gitrepo').val();
too, but did not work
Upvotes: 0
Views: 496
Reputation: 3224
Use this for your script. Note:
var parts = $('#gitrepo').html();
instead of
var parts = $('#gitrepo').attr('value');
<script>
$(document).ready(function(){
$("p").click(function(){
var parts = $('#gitrepo').html();
alert(parts);
return false;
});
});
</script>
Upvotes: 0
Reputation: 298046
.attr('value');
gets the value of the value
attribute. That element has no value
attribute. You're looking for its text:
$('#gitrepo').text()
Upvotes: 3
Reputation: 123739
You would need to use .html or .text instead of .attr('value');
. There is no value attribute for p
tag and value attribute is generally valid for an input elements and for html tags like p
you can use .text()/.html() to extract its contents.
$("p").click(function(){
var parts = $('#gitrepo').text();
alert(parts);
return false;
});
Upvotes: 1