Reputation: 11035
I am just wanting to create something that when clicked will hide or show the text below the item. THE QUESTION that I have is "how would I change the +
sign into a |
sign when the h1
is clicked. The code:
HTML:
<h1>+ Welcome</h1>
<p>This is the welcome greeting</p>
CSS:
p {
display: none;
}
JavaScript:
$( "h1" ).click(function() {
if ($('p').is(':visible')){
$('p').hide();
} else {
$('p').show();
}
});
Upvotes: 0
Views: 141
Reputation: 2681
If you do not want to change the markup and want the flexibility of being able to change the other text in the h1 without modifying your JavaScript, then using the JavaScript replace function could be a alternative.
$( "h1" ).on('click', function() {
var heading = $(this).html();
if ($('p').is(':visible')){
$('p').hide();
$(this).html(heading.replace('|', '+'));
} else {
$('p').show();
$(this).html(heading.replace('+', '|'));
}
});
Upvotes: 0
Reputation: 288510
Try using ::before
pseudo-element:
<div class="wrapper">
<h1>Welcome</h1>
<p>This is the welcome greeting</p>
</div>
.wrapper > h1:before {
content: '|';
}
.wrapper.hide > h1:before {
content: '+';
}
.wrapper.hide > p {
display: none;
}
$(".wrapper").addClass('hide').each(function() {
var $wrapper = $(this);
$wrapper.children('h1').on('click', function() {
$wrapper.toggleClass('hide');
});
});
Upvotes: 2
Reputation: 5752
Here is your code
<h1><span>+</span> Welcome</h1>
<p>This is the welcome greeting</p>
$( "h1" ).click(function() {
if ($('p').is(':visible')){
$('p').hide();
$(this).find('span').text('+');
} else {
$('p').show();
$(this).find('span').text('|');
}
});
Upvotes: 0
Reputation: 481
Try to put the "+" inside it's own <span>
tag.
HTML:
<h1>
<span id='symbol' class="plus">+</span> Welcome
</h1>
jQuery:
$("h1").click(function(){
var span = $("#symbol");
if(span.hasClass("plus")){
span.html("|");
span.removeClass("plus");
span.addClass("pipe");
}else{
span.html("+");
span.removeClass("pipe");
span.addClass("plus");
}
});
Hope it helps!
Upvotes: 1
Reputation: 6253
You should put your +
to an element and change it:
HTML
<h1><span>+</span> Welcome</h1>
<p>This is the welcome greeting</p>
jQuery
$( "h1" ).click(function() {
if ($('p').is(':visible')){
$('p').hide();
$(this).find('span').text('+');
} else {
$('p').show();
$(this).find('span').text('|');
}
});
Upvotes: 3
Reputation: 18123
Since it's the h1
that's clicked, you can use $(this)
inside your function.
$( "h1" ).click(function() {
if ($('p').is(':visible')){
$('p').hide();
$(this).text('+ Welcome');
} else {
$('p').show();
$(this).text('| Welcome');
}
});
Upvotes: 4
Reputation: 1405
You can access the content of a tag with .text()
(doc):
$( "h1" ).click(function() {
if ($('p').is(':visible')){
$('p').hide();
$(this).text("+ Welcome");
} else {
$('p').show();
$(this).text("| Welcome");
}
});
However, as others pointed out, this would be easier if you moved the special character inside its own <span>
element. Also consider giving an id
to the elements you want to interact with, as $("p")
will match all the p
elements in the page.
Upvotes: 1