-1

I want to select #btnsrchicon parent's child #txtSearch. I tried in the below format, but not works

HTML

<div id="topSearch">

<input id="txtSearch" name="search" type="text" value="" id="txtSearch" onClick="ClearTextBox()" onkeypress="searchenter(event);"/>
<button id="btnsrchicon" class="submit" onClick="search_another();"></button>

</div> 

and CSS

<style>
 #txtSearch{display:none;}
#btnsrchicon:hover < #topSearch+#txtSearch
{
 display: block;
 border:2px solid red;
}
</style>
ManiMuthuPandi
  • 1,594
  • 2
  • 26
  • 46

2 Answers2

3

CSS doesn't offer this solution.

What you can do is a javascript solution when user hover elem:

$('#btnsrchicon').prev('#txtSearch').addClass('yourClass')

Something like that:

$('#btnsrchicon').hover(function(){
  $(this).prev('#txtSearch').addClass('show');
}, function(){
  $(this).prev('#txtSearch').removeClass('show');
});
#txtSearch{
  display:none;
}

#txtSearch.show {
 display: block;
 border:2px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div id="topSearch">
  <input id="txtSearch" name="search" type="text" value="" id="txtSearch" onClick="ClearTextBox()" onkeypress="searchenter(event);"/>
  <button id="btnsrchicon" class="submit" onClick="search_another();">Button</button>
</div>

Edit: Another solution

#txtSearch{
  display:none;
}

#btnsrchicon:hover + #txtSearch
{
 display: block;
 border:2px solid red;
}
<div id="topSearch">

<button id="btnsrchicon" class="submit" onClick="search_another();">Button</button>
<input id="txtSearch" name="search" type="text" value="" id="txtSearch" onClick="ClearTextBox()" onkeypress="searchenter(event);"/>

</div> 

... and you adjust the position with CSS.

1

There is no parent selector in CSS. But you can select a following sibling. A possible solution might be to change the order of the button and the input and use the + selector, like that:

#btnsrchicon:hover + #txtSearch {
  display: block;
}

#txtSearch {
  display: none;
}
<button id="btnsrchicon" class="submit" onClick="search_another();">Bouton</button>
<input id="txtSearch" name="search" type="text" value="" id="txtSearch" onClick="ClearTextBox()" onkeypress="searchenter(event);"/>

Then, if the order is really important, you can use some css to visually revert the order

Richard Casetta
  • 346
  • 1
  • 7