Reputation: 87
Can I call a JavaScript function on two different elements with different ids?
HTML
<p id="id1">12</p>
<button type="button" onClick="ABC(document.getElementById('id1').innerHTML)">one</button>
<input type="text" id="id2" value="21"/>
<button type="button" onClick="ABC(document.getElementById('id2').innerHTML)">two</button>
JS
function ABC(id = null) {
if(id) {
$.ajax({
url: 'url.php',
type: 'post',
data: {id: id},
dataType: 'json',
success:function(result) {
}
});
}
}
The function is not working on the second button and I don't know why.
Upvotes: 0
Views: 330
Reputation: 557
the second button use input data so you should use document.getElementById('id2').value
function ABC(id = null) {
if(id) {
$.ajax({
url: 'url.php',
type: 'post',
data: {id: id},
dataType: 'json',
success:function(result) {
}
});
}
}
<p id="id1">12</p>
<button type="button" onClick="ABC(document.getElementById('id1').innerHTML)">one</button>
<input type="text" id="id2" value="21"/>
<button type="button" onClick="ABC(document.getElementById('id2').value)">two</button>
Upvotes: 2