0

javascript:

function mostrar(order) {
switch (order) {
    case 0:
        $(".hide").removeClass("show");
        $(".mostrar1").addClass("show");
        break;
    case 1:
        $(".hide").removeClass("show");
        $(".mostrar2").addClass("show");
        break;
    case 2:
        $(".hide").removeClass("show");
        $(".mostrar3").addClass("show");
        break;
    case 3:
        $(".hide").removeClass("show");
        $(".mostrar4").addClass("show");
        break;
}
}

html:

<a href="" onclick="mostrar(0);">Leer más</a>
<a href="" onclick="mostrar(1);">Leer más</a>
<a href="" onclick="mostrar(2);">Leer más</a>
<a href="" onclick="mostrar(3);">Leer más</a>
<div class="hide mostrar1">asd1</div>
<div class="hide mostrar2">asd2</div>
<div class="hide mostrar3">asd3</div>
<div class="hide mostrar4">asd4</div>

css:

.hide { display: none;}
.show { display:block!important}

http://jsfiddle.net/dn69d14L/

I´m only trying to show and hide the text.

Can someone explain me why is this not working?

2
  • "Uncaught ReferenceError: $ is not defined " Commented Dec 1, 2014 at 13:47
  • This question should be titled: "... using Jquery". Here you are talking about Jquery techniques. Commented Dec 1, 2014 at 13:47

5 Answers 5

1

First your fiddle does not include jQuery so it fails.

Second you do not cancel the click event so the links fire.

Change the links to

<a href="#" onclick="mostrar(0);">Leer más</a>

or

<a href="#" onclick="mostrar(0);return false;">Leer más</a>

or even better attach events with jQuery and use preventDefault()

$(".menu").on("click", "a[data-display]", function (e) {
  e.preventDefault();
  var link = $(this);
  $(".tab-content").addClass("hide");
  $("a.active").removeClass("active");
  link.addClass("active");
  $(".mostrar" + link.data("display")).removeClass("hide");
});
.hide {
    display: none;  
}

.active{ background-color: yellow; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="menu">
  <a href="#" data-display="1">Leer más</a>
  <a href="#" data-display="2">Leer más</a>
  <a href="#" data-display="3">Leer más</a>
  <a href="#" data-display="4">Leer más</a>  
</div>
<div class="tab-content hide mostrar1">asd1</div>
<div class="tab-content hide mostrar2">asd2</div>
<div class="tab-content hide mostrar3">asd3</div>
<div class="tab-content hide mostrar4">asd4</div>

Sign up to request clarification or add additional context in comments.

Comments

0

You need to include a jQuery librairy, or use native JavaScript.

<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>

And, as it's use with a librairy, you need to load your code once the dom is ready:

// Shorthand for $( document ).ready()
$(function() {
    console.log( "ready!" );
    // your code !
});

1 Comment

now i have this error, {"error": "Please use POST request"}! And it refresh me the site after I click the button
0

You've not included jQuery library but that's one of the problems. When you use jQuery it's better not to use inline-js. Also your function could get simpler.

$('a').click(function(){
   $('.hide').hide();
   $('.mostrar' + ($(this).index() + 1))).show();
});

Comments

0

Try this demo: http://jsfiddle.net/dn69d14L/1/

<a href="javascript:void(0)" onclick="mostrar(0);">Leer más</a>
<a href="javascript:void(0)" onclick="mostrar(1);">Leer más</a>
<a href="javascript:void(0)" onclick="mostrar(2);">Leer más</a>
<a href="javascript:void(0)" onclick="mostrar(3);">Leer más</a>
<div class="hide mostrar1">asd1</div>
<div class="hide mostrar2">asd2</div>
<div class="hide mostrar3">asd3</div>
<div class="hide mostrar4">asd4</div>

and of course, use the JQuery library.

Comments

0

Change your HTML to.

<a href="javascript:mostrar(0)">Leer más</a>
<a href="javascript:mostrar(1)">Leer más</a>
<a href="javascript:mostrar(2)">Leer más</a>
<a href="javascript:mostrar(3)">Leer más</a>
<div class="hide mostrar1">asd1</div>
<div class="hide mostrar2">asd2</div>
<div class="hide mostrar3">asd3</div>
<div class="hide mostrar4">asd4</div>

Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.