Finding a specific html element in a htmlCollection array using jquery - javascript

I hope I worded the title correctly.
Basically I am working in a sharepoint environment.
On every page of my site I have a sidebar menu with quicklinks to various other pages.
In my sidbar menu which is mostly, if not entirely, generated by SharePoint each menu item in the list is given the class 'menu-item-text'.
Now there is a specific menu item where I want to, using javascript, append text to the existing text. A counter specifically.
So I have something like this..
MenuItem1
MenuItem2
MenuItem3
MenuItem4
MenuItem5
and I want to do a calculation on page load and display the numeric results of the calculation to MenuItem3, such as..
MenuItem1
MenuItem2
MenuItem3 (5)
MenuItem4
MenuItem5
So I assume the way to do this is via javascript. And normally this would be easy if I could target it directly with an ID, but I have a generic class name like this - <span class="menu-item-text">MenuItem3</span>
If I use document.getElementsByClassName('menu-item-text'), i am given back a 'HTMLCollection[389]'
how can i find the 'MenuItem3' item in this array? I assume I can idenitfy by where the 'innerHTML' of the 'menu-item-text' object is equal to 'MenuItem3'.
But unsure how. :)
Thanks!
EDIT:
Here is what the html looks like on the page for the side bar. There is a lot to it...

Well either your class name or html text should be permanent fixed with html.
If li text are fixed like 'MenuItem3' and having class 'menu-item-text'
then you can do in this way.
var element = $('.menu-item-text:contains("MenuItem3")')
otherwise you should allocated id to each element based on your requirement.
Here is plunker

Related

Changing the URL of an element I cannot edit

I am using a Content Management System that places a title on the top of all our webpages. If you click on this title, it takes you to the main homepage. This part of the page is something I cannot change, but fortunately I can add CSS and/or Javascript to these webpages.
Is there a way to make it so that clicking on that title, it takes me to a different URL? I cannot edit the HTML that is used to display that title (Posted below)
I have honestly not been able to think up of a solution for this. I can only imagine that it's possible through Javascript, but I'm not sure.
<h1 class="page-title">
<span><a data-home-link="" href="/sites/ww/MyWebPage">My Web Page</a></span>
</h1>
I would like to be able to click on the "H1" element and have it take me to a different webpage, different from "MyWebPage".
If you cannot change the html to add an id you could use document.querySelector or document.querySelectorAll
For example, if it's the only 'a' element with that link address on the page you can use:
document.querySelector('a[href="/sites/ww/MyWebPage"]').setAttribute('href', '/elsewhere/page');
Or if it's the 1st of >1 you can use:
document.querySelectorAll('a[href="/sites/ww/MyWebPage"]')[0].setAttribute('href', '/elsewhere/page');
You can simply give your element a unique id and then edit its attributes with javascript. So you will have:
<a data-home-link="" href="/sites/ww/MyWebPage" id = "someId">
And then with javascript, you can change its attributes like so:
document.getElementById('someId').setAttribute('href',
'/sites/someOtherFolder/..');
Should you want to not display the enforced title at all, just use JavaScript or jQuery to hide class "page-title"
Eg with jQuery
$(document).ready(function () {
$(".page-title").hide();
});

Changing the Value of a Span tag using JavaScript via Name

I'm working on making a timer application, but at the moment, I have the timers set to their default values, 5:00, until the image above is clicked. The code for clicking the image is this:
<img src="images/Icons/image.png" id="id">
When that is clicked, I want it to adjust the following span code:
<span id="timer" class="one" name="blue1">5:00</span>
However, there are many parts to the code and with my CSS styles, I can't really spare the ID or class variables, so I'd like to use JavaScript to target this timer by its one unshared name, which is the name="blue1". Here is my JavaScript for now.
function BlueTimer1() {
document.getElementsByName('blue1').innerHTML="blah";
}
Obviously it will actually do something once I am done with it, but for now I just need to be able to modify the text within the span, I don't care what it edits it to.
Thanks for any assistance.
Actually document.getElementsByName('blue1') will return a collection/array like object of spans even there is only one so you need to select the first one using:
document.getElementsByName('blue1')[0].innerHTML="blah";
So it'll work, but if you use getElementById('timer') then you can do it usong:
document.getElementById('timer').innerHTML="blah";
Make sure you have only one element using one id, which means an id must be unique.

What is the simplest way to filter the content of a web page from a drop down menu?

I would like to be able to allow a user to "filter" the contents of an HTML page from a drop down menu.
I have minimal coding skills but maintain a simple website produced using Emacs org-mode. (easy to assemble pages and produce different versions of the same content using tags.) The output is simple HTML.
I can easily produce different versions of a page and make them selectable with a drop down menu to move between them, but this means I have different versions of the same content on my website, which makes retrieval from search engines confusing.
Ideally, I would like user A to be able to select to see the whole page, user B to see some of it, and user C to see most of it except a small portion. This is a convenience to the users (not for security, etc.)
What is the simplest way of implementing this? I realize a web developer would probably use Ajax, etc., but that's not me.
Sounds like you could make use of showing/hiding sections of the page with some DIVs based on a drop down SELECT.
To do this, you wrap the content that you want to filter in some DIVs and create a JavaScript function that "filters" the displayed content based on the value attribute of the SELECT.
Here is a simple example:
HTML
<select id="myDropdown" onchange="filterContent();">
<option value="A">All content</option>
<option value="B">Some content</option>
<option value="C">Little content</option>
</select>
<div id="contentA">
** Content A ***
</div>
<div id="contentB">
** Content B ***
</div>
<div id="contentC">
** Content C ***
</div>
JavaScript
function filterContent() {
var user = document.getElementById("myDropdown").value;
var contentA = document.getElementById("contentA");
var contentB = document.getElementById("contentB");
var contentC = document.getElementById("contentC");
if(user=="A") {
contentA.style.display="block";
contentB.style.display="block";
contentC.style.display="block";
} else if (user=="B") {
contentA.style.display="none";
contentB.style.display="block";
contentC.style.display="block";
} else if (user=="C") {
contentA.style.display="none";
contentB.style.display="none";
contentC.style.display="block";
}
}
Try it here: http://jsfiddle.net/JsZ8S/
Here is another example with multiple different sections that can be shown or hidden based on the selection. Note that the scheme used for IDs is contentA1, contentA2, etc. the letter being the user and the number after the letter is the sequence since IDs must be unique. Also note the difference in the JavaScript code - because we have more sections, we have to account for showing and hiding them in the if/else block: http://jsfiddle.net/JsZ8S/2/
In case you are ready to use jQuery another example is using classes. If you find that you are creating numerous sections and are tired of keeping track of IDs, you might want to use classes. Classes in this case, work like IDs that you can use again and again. You mark any section you want displayed to all users (user A) with class="contentA", any area for users A and B with class="contentB" and everything else just leave unmarked. This is starting to get a bit un-simple at this point but see what you think.
Here is an example (requires jQuery) using classes: http://jsfiddle.net/JsZ8S/5/
You cannot do it with HTML alone. HTML defines a static document with static formatting. You need at least a little bit of JavaScript to dynamically change the page. Otherwise you have to create some sort of link or button that takes the browser to a new page with the desired changes. (This is about how the web worked for the first 5 or so years.)
A small about of JavaScript plus a library like jQuery should make this easy enough to do if you have any programming experience.
HTML is used to just creating the markup and CSS is used to style it. There is no way you can do "filtering" in plain HTML. You will definitely need some JavaScript knowledge. Try your hands on jQuery and angularJS. They are really easy to learn and the documentation is pretty amazing.

How to create interactive tags in html file?

I don't know anything about programming, so I'm trying to find out where to start learning + how difficult my problem is. Since I don't have any programming knowledge, I'll try to describe my problem in natural language, hope that is OK.
I have the html file of the penal code (a type of law). It contains many different rules, that are in numbered paragraphs (§ 1, § 4, etc).
Now I want to look at the source code and manually “tag” the paragraphs according to specific criteria. For example all the paragraphs that concern the use of a weapon get the “weapon” tag, or that have a minimum sentencing of 1 year and higher get a “crime” tag, etc.
At the end I want to view an interactive html file in Firefox/Chrome, where I could for example click on a “crime” button, and all §§§ that were tagged with “crime” would appear in bold red, keeping the rest of the document intact. Ideally I would also be able to click on “weapon” and would only see the §§§ tagged with “weapon”, making the rest of the document disappear.
The function it's just for me, so it would only need to work on a Xubuntu 11.04 desktop with Firefox or Chrome. The original source file would be http://bundesrecht.juris.de/stgb/BJNR001270871.html. The code looks strange to me, is there a way to convert it into something more easily manually editable?
Any help would be greatly appreciated. Primarily I don't know where to start learning. Do I need to know HTML, jQuery, or a programming language like Python? Do I need to set up an Apache server on my PC? Perhaps because of my ignorance of programming, this seems like a not too complex function. Am I mistaken in the belief that an amateur could build something like thins maybe one month?
I think this is not very difficult to make, although the tagging process can be quite labour-intensive.
You don't need much programming skills, especially when you want to tag stuff manually. You probably only need basic HTML and CSS and some Javascript to pull this off.
What I would do is the following
Create a local copy of the HTML file (use Save As in your browser)
Manually tag each § by giving it the appropriate tag as a classname
Create a list of all available tags and let javascript filter out the § you'd like to see
Now Step 1 is pretty easy I guess, so I'll go right to Step 2. The paragraphs in the HTML file are formatted according to a certain pattern, e.g.:
<div class="jnnorm" id="BJNR001270871BJNE009802307" title="Einzelnorm">
<div class="jnheader">
<a name="BJNR001270871BJNE009802307"/>Nichtamtliches Inhaltsverzeichnis
<h3><span class="jnenbez">§ 31</span> <span class="jnentitel">Rücktritt vom Versuch der Beteiligung</span></h3>
</div>
<div class="jnhtml">
<div>
<div class="jurAbsatz">
(1) Nach § 30 wird nicht bestraft, wer freiwillig etc.
</div>
</div>
</div>
</div>
What you want to do now is add your tag to the <div> element with the class jnnorm. So the above example would become (if the tag weapon would be appropriate):
<div class="jnnorm weapon" id="BJNR001270871BJNE009802307" title="Einzelnorm">
You do that for each paragraph in the HTML. This will be pretty boring, but okay.
Now Step 3. First create a list of links of all the tags you've just created. How you create lists in html is explained here. Put this at the top of the HTML document. What you want to do with javascript is when you click on one of the links in your list that only the paragraphs with the given class are shown. This is most easily done with jQuery's click event and the show and hide methods.
Updated with jQuery example
Make a menu like this
<ul id="menu">
<li id="weapon">Weapons</li>
<li id="crime">Crime</li>
</ul>
And then use the following jQuery
<script>
$(document).ready(function(){
// When a <li> element inside an <ul> with the id "menu" is clicked, do the following
$('ul#menu li').click(function(){
// Get the id of the <li> element and append a '.' so we get the right name for the tag (class) we want to show
var tag = '.' + $(this).attr('id');
// Hide all elements of class 'jnnorm'
$('.jnnorm').hide();
// Show all elements with the class name of tag we want
$(tag).show();
});
});
</script>
Note: HTML classes are denoted as .classname in jQuery whereas HTML id's are denoted as #idname.
Good luck!
This could be done using purely HTML/CSS and Javascript, so not server would be needed. JQuery would make the javascript side easier.
Basic idea of how to do it:
Use CSS style classes for your "tags"
Have a button for each tag with an onclick handler that uses JQuery to highlight everything with that tag (or make everything else invisible)
The HTML source code actually looks nicely structured, though it could use a few more linebreaks for sub-paragraphs. Any good HTML/XML editor has an autoformat feature that handles this, though you could get any specific format you want using a programming language with convenient text-manipulation facilities, such as Perl, awk or Python.

How to use onmouseover?

I have a list being displayed on a JSP. On mouse hover on any of the value i need to show a description corresponding that value. Need to show description not as an alert and also cannot make the values as hyperlink.
eg.
suppose the value is ABC so on mouse hover should show AppleBoyCat.
need to use onmouseover. let me know how to do it..
What do you want to do? If you just want to show a tooltip, you can set the title attribute of any element and it will be displayed as a tooltip.
Also, the abbr tag can be used as tooltips too:
<abbr title="test">stuff</abbr>
You can go about it in two ways:
1 - a hidden dom object (a div for instance) which reveals itself when you roll over whatever
or
2 - you can rewrite the html of the particular element you're mousing over.
You can load this data in when you load everything else (either as Javascript objects, or as markup, though that's much bulkier) or you can asynchronously load the description data from a service when you mouse over (though you'll have more lag).
jQuery is a quick and dirty way to achieve this (more quick than dirty), but straight JS or pretty much any other JS library will do as well.
Perhaps not the cleanest solution but something like this:
<a class='hover' rel='tooltip'>Link</a>
//Some hidden div, putting css inline just for example
<div id='tooltip' style='display:none;'>Content</div>
$(function() {
$('.hover').mouseover(function() {
var tooltip = $(this).attr('rel');
$('#' + tooltip).fadeIn();
});
});
And offcourse add a callback hiding it again. It just takes the value from rel of the link and use as an id for the div to show.
This is a quick and dirty solution, can be made alot smoother if you just work with it a little;)
There also alot of plugins out there allowing the same functionality in a cleaner fashion.
*Edit: Just noticed you added a comment on another post that you can't use jQuery.. shouldn't tag a post with something you're not intending to use.
As TJHeuvel already said, you can simply use the title attribute.
Best approach is to build the list with both the value and title attribute from within JSP, if not possible for some reason, you can build client side array of each value and its corresponding description then using JavaScript dynamically assign the title on mouseover.
Show us some more code to get more/better help.
For simple tooltips, the title attribute is most effective, as pointed out by TJHeuvel
If you need more advanced tooltips with HTML and CSS formatting, I'd suggest you use an external library.
One that works nicely without jQuery ist wz_tooltip download here, documentation here
When included correctly, you can add tooltips by calling the functions Tip() and UnTip() as follows:
Homepage

Categories

Resources