0
<style>
     .sys_spec_text{}
     .sys_spec_text li{ float:left; height:28px; position:relative; margin:2px 6px 2px 0; outline:none;}
     .sys_spec_text li a{ color: #db0401; height:24px; padding:1px 6px; border:1px solid #ccc; background:#fff; display:inline-block; line-height:24px;}
     .sys_spec_text li a:hover{ border:2px solid #e4393c; padding:0 5px; text-decoration:none;}
     .sys_spec_text li i{ position:absolute; width:10px; height:10px; font-size:0; line-height:0; right:2px; bottom:2px; background:url(img/sys_item_selected.gif) no-repeat right bottom; z-index:99; display:none;}
     .sys_spec_text li.selected a{ border:2px solid #e4393c; padding:0 5px;}
    .sys_spec_text li.selected i{ display:block;}
</style>


<ul class="sys_spec_text">
    <li ><a href="javascript:;" title="xl">xl</a><i></i></li>
    <li ><a href="javascript:;" title="l">l</a><i></i></li>
</ul>

When the mouse is clicked manually, li border become red, but how to auto click it with JavaScript?

  • 2
    maybe [click()](https://www.w3schools.com/jsref/met_html_click.asp) is what you're looking for? – Snel23 May 23 '19 at 01:43
  • 1
    do you want to auto click the first one, the second one...? should it be auto clicked on page load or when something else happens? please be a bit more specific – Scaramouche May 23 '19 at 02:56
  • @Snel23 click() wont work – lolololhahahahaha May 23 '19 at 03:28
  • "when mouse click li border become red" , is so, it isn't with the code you have provided: https://jsfiddle.net/z2gw9o7c/ . Hover yes, click - no. What else have you not shown us? – Jon P May 23 '19 at 03:58
  • Can you elaborate your question, as the solution may lie in CSS itself if you just want to highlight border of first `li` using `li:first-child` code – Learning May 23 '19 at 04:43
  • Also, from a semantics perspective, if the links have no href, then it should be buttons, not links. – Capsule May 23 '19 at 04:45
  • You can use trigger method for auto click li something like that $( "#foo" ).trigger( "click" ); – Videsh Chauhan May 23 '19 at 06:11

3 Answers3

0

Please make it more clear. As I see from css, you show li border when it hovered. But then you ask about how to make the same with click. So it will lead to the situation when the element got border and further stay with it because there is no event to remove it.

Also you can use :active pseudo-class for anchor element that is the same as click js event.

If you want to immitate hover event you can listen to 'mouseenter'/'mouseleave' events to make the same as you did with css.

let listEls = [...document.querySelectorAll('.link')]
let firstLi = listEls[0]


/* === CLICK SECTION === */

// This is "click" event listener
listEls.forEach(e => {
  e.addEventListener('click', event => {
    console.log('link is clicked', event)
    event.target.classList.add('selected')
  })
});

// here we manually trigger "click" event on the first li

firstLi.click()

// The problem is that "selected" class has been added but if there is no method to remove it. Let's fix it

document.addEventListener('click', e => {
  console.log(e)
  if (!e.target.classList.contains('link')) {
    listEls.forEach(el => el.classList.remove('selected'))
  }
})

// Now if any li was clicked and has "selected" class, you can click anywhere else to remove it 

/* === END CLICK SECTION === */


/* === HOVER SECTION === */

// To se how hover works comment the CLICK SECTION above and uncomment this HOVER SECTION
// DO not forget to comment hover rule for link in css

/*
listEls.forEach(e => {
  e.addEventListener('mouseenter', e => {
    console.log(e)
    e.target.classList.add('selected')
  })
});

listEls.forEach(e => {
  e.addEventListener('mouseleave', e => {
    e.target.classList.remove('selected')
  })
});
*/

/* === END HOVER SECTION === */
.sys_spec_text {}

.sys_spec_text li {
  float: left;
  height: 28px;
  position: relative;
  margin: 2px 6px 2px 0;
  outline: none;
  list-style-type: none;
}

.sys_spec_text li a {
  color: #db0401;
  height: 24px;
  padding: 1px 6px;
  border: 1px solid #ccc;
  background: #fff;
  display: inline-block;
  line-height: 24px;
}

/* If you want to check mouseenter/mouseleave events comment following block */
.sys_spec_text li a:hover {
  border: 2px solid #e4393c;
  padding: 0 5px;
  text-decoration: none;
}

/* OR  you can use :active pseudo-class, so then you have to disable js "click" event listener and comment :hover block above */

/*
.sys_spec_text li a:active {
  border: 2px solid #e4393c;
  padding: 0 5px;
  text-decoration: none;
}
*/

.sys_spec_text li a.selected {
  border: 2px solid #e4393c;
  padding: 0 5px;
  text-decoration: none;
}

.sys_spec_text li i {
  position: absolute;
  width: 10px;
  height: 10px;
  font-size: 0;
  line-height: 0;
  right: 2px;
  bottom: 2px;
  background: url(img/sys_item_selected.gif) no-repeat right bottom;
  z-index: 99;
  display: none;
}

.sys_spec_text li i {
  position: absolute;
  width: 10px;
  height: 10px;
  font-size: 0;
  line-height: 0;
  right: 2px;
  bottom: 2px;
  background: url(img/sys_item_selected.gif) no-repeat right bottom;
  z-index: 99;
  display: none;
}
<ul class="sys_spec_text">
  <li><a class="link" href="javascript:void(0)" title="xl">xl</a><i></i></li>
  <li><a class="link" href="javascript:void(0)" title="l">l</a><i></i></li>
</ul>
Timur Gilauri
  • 824
  • 8
  • 13
0

This will do what you want.

In CSS, define .clicked style with red border.

In javascript, add listeners to detect clicks, which then adds the class clicked to the element.

document.querySelectorAll("li").forEach(li => {
   li.addEventListener("click", function(event)  {
       event.target.classList.add("clicked");
   });
});
<style>
    .sys_spec_text{}
    .sys_spec_text li{ float:left; height:28px; position:relative; margin:2px 6px 2px 0; outline:none;}
    .sys_spec_text li a{ color: #db0401; height:24px; padding:1px 6px; border:1px solid #ccc; background:#fff; display:inline-block; line-height:24px;}
    .sys_spec_text li a:hover,
    .sys_spec_text li .clicked { border:2px solid #e4393c; padding:0 5px; text-decoration:none;}
    .sys_spec_text li i{ position:absolute; width:10px; height:10px; font-size:0; line-height:0; right:2px; bottom:2px; background:url(img/sys_item_selected.gif) no-repeat right bottom; z-index:99; display:none;}
    .sys_spec_text li.selected a{ border:2px solid #e4393c; padding:0 5px;}
    .sys_spec_text li.selected i{ display:block;}
</style>
<ul class="sys_spec_text">
    <li ><a href="javascript:;" title="xl">xl</a><i></i></li>
    <li ><a href="javascript:;" title="l">l</a><i></i></li>
</ul>
chatnoir
  • 2,185
  • 1
  • 15
  • 17
-2

Use setInterval() method and check periodically and fire the event accordingly.

Just code
  • 13,553
  • 10
  • 51
  • 93