Function does not run 2x

Asked

Viewed 41 times

1

I have a Function selectStep() who receives a onclick per parameter 1 or 2, it spins what was past. The impasse is that I have two Divs Content1 and Content2, both with the same thing. But only the first wheel, the second does not repeat.

I am beginner, I appreciate any kind of help, criticism. Thank you!

SOURCE

2 answers

1

When I have this kind of situation, I prefer to use a date attribute to define the target element, as in the example below:

$('[data-target]').click(function(event) {
  event.preventDefault();
  $('.content').hide();
  $($(this).data('target')).show();
});
.content {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content" id="content1">Lorem Ipsum 1</div>
<div class="content" id="content2">Lorem Ipsum 2</div>
<a href="#" data-target="#content1">1</a>
<a href="#" data-target="#content2">2</a>
<a href="#" data-target=".content">Todos</a>

1

Your code is correct and works with small adjustments in jsFiddle.

  • don’t use the onLoad jsFiddle, it puts your code inside a function and the functions you had stop being global and HTML stops finding them. Same problem that this other question.
  • add jQuery to jsFiddle, your code needs to load the library

Working: https://jsfiddle.net/m2uogLf7/2/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.