You have two problems:
First of all, your onClick function has a elem parameter, but you never pass it in. To do that, you need to use onclick="onClick(this)" in the HTML, that way when the onClick function is called, you would get the current element as the argument.
function onClick(elem) {
var $this = elem.tagName;
console.log(elem);
}
<div class="items">
<input type="" name="" id="keywordSearch1" placeholder="">
<button onclick="onClick(this)">button1</button>
</div>
Second problem is that you are taking elem.tagName which would be a string and treating it as if it's a jQuery object.
You should either include jQuery and use that as appropriate. You also need to specify type="text" in order to use the input[type=text] selector:
function onClick(elem) {
var $this = $(elem); //< -- wrap the element in a jQuery wrapper
var val = $this.siblings('input[type=text]').val();
if (val == '') {
console.log('no input');
} else {
console.log(val);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="items">
<input type="text" name="" id="keywordSearch1" placeholder="">
<button onclick="onClick(this)">button1</button>
</div>
Alternatively, you can do the same without jQuery using pure JavaScript with minor changes to get the same effect:
function onClick(elem) {
//get the parent
var parent = elem.parentNode;
var val = parent
.querySelector('input[type=text]')//search the parent's children to simulate sibling searching
.value; //the non-jQuery way to get the value
if (val == '') {
console.log('no input');
} else {
console.log(val);
}
}
<div class="items">
<input type="text" name="" id="keywordSearch1" placeholder="">
<button onclick="onClick(this)">button1</button>
</div>
Finally, having an in-line onclick specified in HTML is generally a bad idea. The preferred way is to attach event listeners via JS. In jQuery, you can do that using .on:
function onClick(elem) {
var $this = $(elem);
var val = $this.siblings('input[type=text]').val();
if (val == '') {
console.log('no input');
} else {
console.log(val);
}
}
//attach the click listerner
$('button').on('click', function() {
onClick(this);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="items">
<input type="text" name="" id="keywordSearch1" placeholder="">
<button>button1</button>
</div>
<div class="items">
<input type="text" name="" id="keywordSearch2" placeholder="">
<button>button2</button>
</div>
<div class="items">
<input type="text" name="" id="keywordSearch3" placeholder="">
<button>button3</button>
</div>
Or in pure JavaScript:
function onClick(elem) {
var parent = elem.parentNode;
var val = parent
.querySelector('input[type=text]')
.value;
if (val == '') {
console.log('no input');
} else {
console.log(val);
}
}
//attach the click listerner to each element
var buttons = document.getElementsByTagName('button');
for(var i = 0; i < buttons.length; i++) {
var button = buttons[i];
button.addEventListener("click", function() {
onClick(this);
})
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="items">
<input type="text" name="" id="keywordSearch1" placeholder="">
<button>button1</button>
</div>
<div class="items">
<input type="text" name="" id="keywordSearch2" placeholder="">
<button>button2</button>
</div>
<div class="items">
<input type="text" name="" id="keywordSearch3" placeholder="">
<button>button3</button>
</div>
Do note, that if you are attaching the event listener directly, then you can even omit passing in this as an argument:
- in jQuery :
$("button").on("click", onClick)
- in plain JS, it's a similar situation, you would do
button.addEventListener("click", onClick)
Once you do that, you have more options at your disposal:
- a function executed as an event listener will have the
this context set as the element that it was initiated from. So, you can directly bind the listener
- in jQuery:
var $this = $(this).
- in plain JS:
var parent = this.parentNode
- the event listener will also be passed in the
event that the listener responded to. From there you can find the original target that was clicked:
function onClick(event) {
var elem = event.target;
}
$this = elem.tagNamewill make a variable that is a string.$this.siblingsseems to be trying to treat it as a jQuery object.