Simple question.
<input type="text" value="Some text" name="thisname" id="thisid" />
How can I get the name of the id or name that is being used, instead of value.
It is clear that value of this text-box is “Some text”. But what I want is name of id and name being used now.
To be more clear … I want “thisid” or “thisname”.
See my code
<!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>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
$(document).ready(function() {
$("p").parent(".selected").css("background", "#FF8877");
});
function add(name) {
$("#" + name).parent().clone().insertAfter($("#" + name).parent());
}
</script>
</head>
<body>
<div><p>Hello</p></div>
<div class="selected"><p name="name" id="name" onclick="add(this.name)">Hello Again</p></div>
</body>
</html>
Any idea ?
Looking at your edited question. You’re doing it wrong.
It’d be much simpler to pass a reference to the element, and not it’s name.
js:
html:
If you want to know why your code didn’t work:
“id” and “name” are 2 different attributes. In CSS (and therefore in jquery) you generally select an element by ID, with this syntax:
#idOfElement. If you really have to, you could select one or more elements by name:*[name="nameOfElement"].this gives you 2 options:
pass to your original
addfunction the id, not the name:change your add function to work with names instead
Anyway, passing a reference of the element itself, is still the best option.