Adding an Accordion-Like Setup? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I want to be able to add the whole Accordion Aspect to this code in jsFiddle. To where if you expand one, and then click on another it collapses the previous div and displays the most recently clicked.
Any idea of how I could accomplish this through this jsFiddle?
http://jsfiddle.net/zrbFE/
Thank you all!

http://jsfiddle.net/fAmWx/1/
This might help get you started in the right direction. The idea is to simplify and reuse class names and code.

I made this some time ago, think it will suit your needs: http://jsfiddle.net/aCaEG/2/

I like this library for that functionality:
http://flowplayer.org/tools/demos/tabs/accordion.html
Way lighter than jQuery UI, and works beautifully. Check that page for examples of how to mark it up and call it in your script.

Try this for your jsfiddle:
function toggleMe(me) {
var alreadyOpen = me.is(':visible');
jQuery('div[class^="content-"]').hide('fast');
if (!alreadyOpen) {
me.show('slow');
}
}
jQuery('.expand-one').click(function() {
toggleMe(jQuery('.content-one'));
var img = $(this).find('img'),
src = img.attr("src"),
alt = img.data("altsrc");
img.attr("src", alt).data("altsrc", src);
});
/// SECOND SESSION:
/////////////////////////////////////////////////////////////////////////////////////
jQuery('.expand-two').click(function() {
toggleMe(jQuery('.content-two'));
var img = $(this).find('img'),
src = img.attr("src"),
alt = img.data("altsrc");
img.attr("src", alt).data("altsrc", src);
});
Hope this helps,
Pete

Related

Change the text of links with an specific class, using javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am looking for a way to replace the text of all the links with an specific class, using javascript (not jquery). I would like all the links with that specific class, to have a generic text such as: "click here".
I would use getElementsByClassName to acquire an array of the links. You can then loop through these elements making any changes required.
Heres a Fiddle that also includes some validation to only chenge the text of anchor tags in case you use targetClass for anything else in other elements.
EDIT: looks like other beat me to it :-) (also fixing type in function)
var links = document.querySelectorAll(".yourclass");
for (var i=0; i<links.length; links++)
{
links[i].innerHTML = "click here";
}
This should do the trick. It's using ES6, so make sure you're using either Chrome Canary or a transpiler like Babel.
Say this is your markup...
<a class="classname" href="#">STUBBED</a>
<a class="classname" href="#">MOCKED</a>
<a class="classname" href="#">PLACEHOLDER</a>
This as your JS should work...
let refs = document.getElementsByClassName('classname');
refs = [].slice.apply(refs);
refs.forEach(ref => ref.innerHTML = 'click here');
function myFunction() {
var c = document.querySelectorAll(".example>a");
for(i=0;i<c.length;i++)
{
c[i].innerHTML = "Click Me!";
}
}
This should do the trick in pure javascript

With jQuery, can you add an HTML element like this? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm doing this tutorial and this guy says, "I'm going to create a div..."
$("document").ready(function() {
// fetch the AJAX content
$("#newsContent").load("news.txt");
//$.getJSON("news.json", successFn);
});
function successFn(result) {
$.each(result.newsStories, function(i, item) {
// Didn't quite understand what this line did.
var newsDiv = $("<div class='news'>");
newsDiv.append(item.title);
newsDiv.append(item.content);
// Now understand that this puts the above div in play.
$("#newsContent").append(newsDiv);
});
}
I'm really only worried about this line:
var newsDiv = $('<div class="news">');
I've tried this on jsfiddle, but it doesn't seem to work. This is happening during the construction of an AJAX request if that helps.
My question is, when does newsDiv become part of the DOM?
$('<div class="news">'); will return $ wrapped object corresponds to an html element (in this example, div element) to you. But not created any element at your html. You can simply add this newly created element to your html like,
$('#myNewsContainer').append(newsDiv); // append to the div with id 'myNewsContainer'
$('#myNewsContainer').html(newsDiv); // replace all html with new div
The line
var newsDiv = $('<div class="news">');
creates a variable newsDiv with value <div class="news">. At this moment it is not appended to html. You can append it to HTML using Jquery append() method.

How to remove script tag from html using javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to remove script tag from Html Code using JavaScript.
Here is the HTML Code :
<script type='text/x-template-handlebars' id='carousel_ui_buttons_next-nav_next'>
<button class="next nav" asg-button>{{{button_text}}}</button>
</script>
I want to remove script tag so the remaining part would be html only.
I mean code should be changed inside browser.
it would be better if you provided us with the code you currently have to help you understand what happened rather than using the code copy/paste ,anyway to fire the js code you first decide when you want it to kick in
when the page finish loading ?
$(window).load(function() {
$('#carousel_ui_buttons_next-nav_next').remove();
});
or when the page is ready
$(document).ready(function($) {
$('#carousel_ui_buttons_next-nav_next').remove();
});
or if you prefer you can make it as a function and call it as much as
you want instead of repeating the same code over and over everywhere
function hideScript() {
$('#carousel_ui_buttons_next-nav_next').remove();
});
// then use it like //
$(document).ready(function($) {
hideScript();
});
the above code use JQuery which is much easier than vanilla js to understand and to work with.
The content of your <script> tag is invalid Javascript, but here is one possible way of achieving what you are after:
// Function to convert an HTML string to a DOM element
String.prototype.toDOM = function () {
var d = document,
i,
a = d.createElement('div'),
b = d.createDocumentFragment();
a.innerHTML = this;
while (i = a.firstChild) {
b.appendChild(i);
}
return b;
};
// The <script> we wish to replace
var st = document.getElementById('carousel_ui_buttons_next-nav_next');
// Replace it with the <button> that is inside of it
st.parentNode.replaceChild(st.innerHTML.trim().toDOM(), st);

jQuery count div, divided into two and add between them [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Tell me how to make a jQuery:
count <div class="one">, divided into two and add between them <div class="two">
/div class="one"/ can be a different number
JsFiddle
You want to add a div after the half of the one divs:
var count = Math.floor($('.one').length / 2);
$('.one').eq(count).after('<div class="two">d</div>');
Demo: https://jsfiddle.net/c8b8tnm3/2/
You can use a class selector to find all the one elements, then use its count(length property) to find its middle and insert the new element
var $ones = $('.one');
$ones.eq(Math.floor(($ones.length-1)/2)).after('<div class="two"></div>')
Demo: Fiddle
Try this, this may help you.
var ones=$('.one');
var indexofOne=$('.one').length/2;
indexofOne=Math.floor(indexofOne);
var insertPosition=$('.one')[indexofOne];
$(insertPosition).after($('<div class="two">'));
JSFiddle

Convert IF to TOGGLE [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have code but it works with scroll height, i want to do something different. I want to convert it to Toggle, is it possible? First click add .animated and remove .fix, on second click remove .animated and add .fix.
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll > 20) {
$header.addClass('animated').removeClass('fix');
} else {
$header.removeClass('animated').addClass('fix');
}
});
I'm new on javascript can anyone help me please?
Thanks!
The toggleClass accepts a switch argument, a Boolean value. true adds the className and false removes the className.
var bool = scroll > 20;
$header.toggleClass('animated', bool).toggleClass('fix', !bool);
I've found solution like this, maybe somebody uses it.
$('.header').on('click', function(e) {
$('.header').toggleClass("animated") .removeClass('fix');
e.preventDefault();
});

Categories

Resources