Reputation: 646
i have a link that contain some data
eg
<li><a href="" onclick="getcategory(this);"><?php echo $result22['category']; ?></a></li>
i want this link to pass this $result22['category'];
value to the ajax function
am trying this
<script type="text/javascript">
function getcategory(cat)
{
var id = cat.value;
alert("hi" + id);
}
</script>
but its shows hi undefined in alert box
what am doing wrong ?
am not getting correct value of $result22['category'];
in alert box
Upvotes: 0
Views: 5044
Reputation: 2791
<a href="" onclick="getcategory(this); return false;">hello</a>
<script type="text/javascript">
function getcategory(ele) {
var id = ele.innerHTML;
alert(id);
}
</script>
Upvotes: 0
Reputation: 289
I would recomend you output into the function if your are not using a separate javascript file to bind the onClick handler that is :
<li>
<a href="" onclick="getcategory('<?php echo $result22['category']; ?>');">
<?php echo $result22['category']; ?>
</a>
</li>
Upvotes: 0
Reputation: 2363
Try this..
<li><a href="" id=<?php echo $result22['category']; ?> onclick="getcategory(this.id);"><?php echo $result22['category']; ?></a></li>
<script type="text/javascript">
function getcategory(cat)
{
//var id = cat.value;
alert("hi" + cat);
}
</script>
Upvotes: 0
Reputation: 4637
Why not just do this?
<li><a href="" onclick="getcategory('<?= $result22['category']; ?>');"><?php echo $result22['category']; ?></a></li>
<script type="text/javascript">
function getcategory(content) {
alert(content);
}
</script>
Upvotes: 0
Reputation: 166071
Since cat
is an a
element, it won't have a value
property. Use textContent
or innerText
(or innerHTML
if there could be child elements):
function getcategory(cat) {
var id = cat.textContent;
alert("hi" + id);
}
It's generally only form controls that have a value property (the input
element for example).
Upvotes: 3