0

This has been answered some times but mine is a different approach.

I want to toggle (via jQuery) a text on click. But I want to have this text defined in the html (because it is multilanguage and it depends in langs, more preciselly in Smarty variables) so the $(this).html(condition ? "Some" : "Other" ) doesn't work.

My actual approach is:

HTML

<a class="readmore" href="#content">
    <span class="text" rel="Menos">Más</span>
</a>

JS

$(".readmore").click(function(event) {
    var contentSelectAnt = $(this).find(".text").html();
    var contentSelect = $(this).attr("rel");
    $(this).find(".text").html(contentSelect);
    $(this).attr("rel",contentSelectAnt);
)};

But I don't quite like the redundancy of the code and the unapropiate use of the rel attribute. I am sure there is a more jquery style solution.

Thanks for reading.

0

3 Answers 3

1

You can attach data via the data method. However, you still have the problem of the initial and opposing state.

<a href="#content" data-content="SomeOtherContent">Text</a>

$('a').click(function()
{
     var currentObj = $(this);
     var currentText = currentObj.html();
     currentObj.html(currentObj.data('content'));
     currentObj.data('content', currentText);
});
Sign up to request clarification or add additional context in comments.

4 Comments

I am testing this now. Will the data attributes give problems in older browsers?
data is jquery so it should be cross browser OK.
Works great! A Frédérik Hidis posted the same answer but more specific to the code in the question, but he deleted it. Thank you for your time.
Edited answer to apply to the question's code, hope this passes.
1

I'm actually struggling to understand what you are trying to do, but cobbled together a DEMO

Don't know if thats what you mean but that was my understanding of the question.

EDIT:

DEMO 2 with the proper functionality?

DEMO 3 without the use of anchor tags.

3 Comments

upvote for the jsFiddle (although it is not what I am looking for, thankyou)
Ah...I understand what you mean now. But I would go along the same lines as Brian Fisher below. Something like this jsfiddle.net/aTLRU/3 for multilanguage support.
You could even go without the anchor tags if you wanted. jsfiddle.net/aTLRU/4
0

How about just using two spans:

<a class="readmore" href="#content">
    <span class="text">Más</span>
    <span class="text" style="display: none;">Menos</span>
</a>

With javascript:

$(".readmore").click(function(event) {
    $(this).find('.text').toggle();
    return false;    
 });

Here is a demo, http://jsfiddle.net/Fq4bT/

5 Comments

I would write it $(".readmore").click(function(event) { $('.text').toggle(); return false; }); OR $(".readmore").click(function(event) { $('#more, #less').toggle(); return false; });
to make is "safer" you could select the children of the clicked as well. (so if there are more '.readmore', it works on just this one instance.
Yeah I agree, I just wanted to have a working sample at first. But when I went to simplify the code as you suggested in your first comment I also added the find on the the clicked element to limit the search to the descendants.
This one's good but I don't like to have the two spans, one with the inline style. Thank you.
You don't need to use an inline style you can use an external stylesheet by adding a class to the hidden span or without adding anything and using a.readmore span:last-child {display: none;}

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.