Solved

Trying to Add a link within a div tag with javascript

Posted on 2013-05-23
4
240 Views
Last Modified: 2013-05-28
Hello,

I have been tasked with adding a link to our LMS (learning management system). We do not have accesses to the html/php files, but the company does allow us to include our own js and css files. I have created a js file to search through all the div's on the page to find the one I am looking for, and than add the link within the div. Code is below.

var div = document.getElementByClassName("login-options");
for(var i=0; i<divs.length; i++) {
	if (div == "login-options")
	{
		var a = document.createElement('a');    
		a.title = "Canvas Student Resource Center";            
		a.innerText = a.title;            
		a.href = "https://coconino.instructure.com/courses/923533";            
		div.appendChild(a);
	}
}

Open in new window


Thanks,

Neo
0
Comment
Question by:Morgan
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 200 total points
Comment Utility
using jquery:
var link = "<div> <a title=\"Canvas Student Resource Center\" href=\"https://coconino.instructure.com/courses/923533\">Canvas Student Resource Center</a></div>";
$('.login-options').append(link);

Open in new window

0
 
LVL 2

Assisted Solution

by:laurent_roy
laurent_roy earned 300 total points
Comment Utility
Hello,

- if it is implemented in your browser, the function is document.getElementsByClassName with an "s" at Elements. But because it's not implemented in all browsers I recommend you to use document.getElementsByTagName . You will get more results but you will avoid problems. If the div you want to retrieve has an id, you can use document.getElementById (without s).
- you made a confusion between div and divs
- div == "..." doesn't mean anything. Do you mean div.className="..." ? (if you use document.getElementsByClassName, you dont't need anything because you already only retrieve divs with className "..."
- a.innerText doesn't work, use a.innerHTML instead

to sum up :

var divs = document.getElementsByTagName("div");
for(var i=0; i<divs.length; i++) {
	if (divs[i].className == "login-options")
	{
		var a = document.createElement('a');    
		a.title = "Canvas Student Resource Center";            
		a.innerHTML = a.title;            
		a.href = "https://coconino.instructure.com/courses/923533";            
		divs[i].appendChild(a);
	}
}

Open in new window

best regards

Laurent
0
 
LVL 1

Author Comment

by:Morgan
Comment Utility
Thank you both. Because of the long weekend I'll take a look at both solutions on Tuesday to see which will fit my needs, though I would assume both would?

Laurent, thank you for taking the time to explain what I was doing wrong. It is appreciated!
0
 
LVL 1

Author Comment

by:Morgan
Comment Utility
Hello Laurent & Sedgwick,

Come to find out I had to go this route (jQuery still):

jQuery(function($){
  $('#pseudonym_session_remember_me').parent().append('<p><a id="training_course" href="MY_LINK"><b>TITLE_OF_LINK</b></a>');
});

Open in new window


I have gone ahead and rewarded points to both of you, with Laurent receiving more as he/she went into more detail on what was wrong with my code.

Again than you both for your help!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now