How to access an element inside a div in jQuery? - html

here's my html code :
<section>
<div class="article">
<p>
things aren't thing anymore they basically copy of originally thing created bu someone no one know as now we can calll them copy's
</p>
</div>
</section>
<br>
<section>
<div class="article">
<p>
things aren't thing anymore they basically copy of originally thing created bu someone no one know as now we can calll them copy's
</p>
</div>
</section>
<br>
<section>
<div class="article">
<p>
things aren't thing anymore they basically copy of originally thing created bu someone no one know as now we can calll them copy's
</p>
</div>
</section>
that my jquery code :
$(document).ready(function(){
$(".article").append('<span class="close" style="top:0px;background:#000;color:#fff;padding:10px;cursor:pointer;">X</span>');
$(".close").click(function(){
$(this).parents(".article > p").hide();
});
});
I want to hide <p> when the user click on span , how can i have access to <p> only by jquery instead of typing <span>X</span> 3 times in html ?

The issue here is you can't use parents() to look up to a parent and have that same selector look inside the parent
Some alternatives are use find() or siblings():
// go up to the parent then find the descendents inside that parent
$(this).parents(".article").find("p").hide();
// OR the span is a sibling of the `<p>` and can target them directly
$(this).siblings("p").hide()// or toggle() if want to show again on alternate clicks

Try using find().
parents() uses filter() under the hood to check whether the node matches the selector. Here the p node matches .article > p, but its not one of the parents of .close
$(document).ready(function() {
$(".article").append('<span class="close" style="top:0px;background:#000;color:#fff;padding:10px;cursor:pointer;">X</span>');
$(".close").click(function() {
$(this).parents(".article").find('p').hide();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section>
<div class="article">
<p>
things aren't thing anymore they basically copy of originally thing created bu someone no one know as now we can calll them copy's
</p>
</div>
</section>
<br>
<section>
<div class="article">
<p>
things aren't thing anymore they basically copy of originally thing created bu someone no one know as now we can calll them copy's
</p>
</div>
</section>
<br>
<section>
<div class="article">
<p>
things aren't thing anymore they basically copy of originally thing created bu someone no one know as now we can calll them copy's
</p>
</div>
</section>

Related

Is there a way to use the same ID multiple times using CSS?

<p id = "formatOne">My text here</p>
<!--insert bunch of other stuff in between-->
<!--Reuse formatOne so I don't have to copy-paste the entire CSS formatting again for each use
(similar to functions in most programming languages, write once, call whenever-->
<p id = "formatOne">Different text here</p> <!--like this, but this is obviously wrong since ID must be unique-->
Is there a way to make CSS ID's that are callable like functions?
id should be unique per HTML element. If you want to apply same style to different HTML elements, you can create class and apply the same class to multiple HTML elements.
.formatOne {
color:blue;
}
<p class="formatOne">My text here</p>
<p class="formatOne">Different text here</p>
This is not possible with ids. If you want same multiple uses in css then only class
allow you to do so.
<body>
<div class="asd">
<h1> First </h1>
<p> First line Statement </p>
</div>
<div class="asd">
<h1> Second </h1>
<p> Second line Statement </p>
</div>
<style>
.asd {color : red;
font-size: 20px}
</style>
</body>

Page loads within a page, rather can completely reloading

I wrote some custom HTML code in my Wikidot article - instead of the "original" Wikidot syntax, I have to use <a href="/page"> for links.
The content of my custom HTML block is like this:
<section class="intro">
<div class="container">
<h1>Headline-line text</h1>
</div>
</section>
<section class="timeline">
<ul>
<li>
<div>
<time>Time value </time> Text. Link here.
</div>
</li>
</ul>
</section>
The problem is that it loads the entire content of the HTML into that carefully selected small portion of the original site.
I can only assume that it has something to do with <div>s, as I've already seen this issue on other sites. Hence my assumption is that there must be a general source of this issue, and this is why I'm asking.
What's the reason of this problem and how can I avoid it?

Dynamic Bootstrap Well

I have a template that gets all of the information generated from an external program. I am attempting to make this template a bit more modern and less 1992.
The page used to work with a table. It didn't look very nice so I am attempting to remove the table and just have everything on the screen fluid.
For the containers of the text I have used the bootstrap well class. The problem is that if one well is a bit longer then the other, it looks very odd. I need both of the <div class="well"> to be the height if the tallest one.
I have tried to do this with a lot of different CSS. I really don't want to have to go back to a table.
The code is below.
<div class="col-md-12">
<div class="col-md-6">
<div class="well">
<h3>Heading</h3>
Info
<br>
<br>
</div>
</div>
<div class="col-md-6">
<div class="well">
<h3>Heading</h3>
Info
<br>
<br>
</div>
</div>
</div>
Any help is appreciated.
so what you want is that the div's height must equal to the div that has a lot of data...
try using jquery.
var maxHeight = Math.max.apply(null, $(".well").map(function ()
{
return $(this).outerHeight();
}).get());
$('.well').height(maxHeight);
check this out:
https://jsfiddle.net/x3ehq57g/2/

Match page source tags with regex

I am trying to catch a tag from a page source with regex.
After allot of trying i find it very hard to establish.
Here is an example of an HTML source:
<div class="searchBx">
<div>
<li>somthing</li>
</div>
</div>
<div>
<li>somthing2</li>
</div>
I am trying to catch only the (div class="searchBx") tag and the tags inside.
It is hard because it always catch the div tag after him.
The result should be:
<div class="searchBx">
<div>
<li>somthing</li>
</div>
</div>
Thanks ahead.
It is impossible for regex to match the div you speak of.
Since the div contains another div, by nature it will not be able to differentiate between the </div> tag within it, or the </div> tag that closes the div you wish to match.
<div class="searchBx">
<div>
<li>somthing</li>
</div> <!-- This -->
</div> <!-- and this are the same to regex -->
<div>
<li>somthing2</li>
</div>
Here's what happens: http://regexr.com/3d0jn
For what you need to do, you must use a DOM parser in whichever language you are using.
Plus it's incredibly poor practice using regex to parse HTML, but everyone does it anyway.

How to make a whole <section> element's area clickable?

I'm working with a codebase where there is a list of articles rendered inside a section element that is wrapped by a link, like this:
<a href="link/to/article">
<section>
<h2>Article title</h2>
<img src="path/to/article/img">
<p>Short description</p>
</section>
</a>
The content inside the section tag is the title, the image and a short description. All the article previews are rendered like this in a tabular/grid-like layout. When a section is clicked, the whole contents of the article are rendered in a new page.
The reason for this is that the whole section area should be clickable. Since this markup doesn't seem too semantic for me, I was wondering if this code is correct, and if it is not, is there a better approach to make a whole section clickable?
What you've done is fine and correct and works well. The W3C even states:
The a element may be wrapped around entire paragraphs, lists, tables,
and so forth, even entire sections, so long as there is no interactive
content within (e.g. buttons or other links).
They include an example similar to yours.
You can use JQuery as such to achieve that.....No need using the link tag
<section id="clickable_section">
<h2>article title</h2>
<img src="article/img">
<p>Short description</p>
</section>
<script>
$(document).ready(function() {
$("#clickable_section").click(function() {
//Do something....
//Continue doing something,like redirecting the user to the new page
});
});
</script>
Having an anchor work like that is pretty kludgey. Might be better to do something like this:
html:
<section data-link="https://www.google.com">
<h2>article title</h2>
<img src="path/to/article/img">
<p>Short description</p>
</section>
css:
section {
cursor: pointer;
}
javascript (jQuery):
$('section').on('click', function(e){
e.preventDefault();
window.location.href=$(this).data('link');
})