Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Trying to Add a link within a div tag with javascript

Posted on 2013-05-23
4
243 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
ID: 39192729
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
ID: 39193641
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
ID: 39196644
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
ID: 39202059
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

860 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