Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have the following code
$var = hello;
echo "<a href='#".$var."' class='test'>Link</a>";
and I wanted that when I click on the link the href value shows up inside a div in the same page.
<div>
// result "#hello" showing up here
</div>
How can I do this ? Should I use POST and GET ?
Can you give me any example ?
Thanks
Firstly you need to set some ID or ClASS for this div, and after use just jQuery:
$(document).ready(function() {
$('.test').click(function() {
var attr = $('.test').attr('href');//here you take attribute and insert it into div
$('#divId').text(attr);
});
});
<div id="divId"></div> //here will be your href value
Try
$(".test").on('click', function() {
$("div#target").html($(this).attr('href') );
}
where target is the id of your div.
you can use jquery :
echo "<a href='#".$var."' class='test'>Link</a>";
$(document).ready(function(){
$('.test').click(function(e){//click event of link
var href = $('.test').attr('href');//get value of href
$("#my_div").html('href');//show value of href on div
return false;
});
});
Make the id of div :
<div id="my_div"></div>
If you use jQuery you can watch the hashchange event like this:
$(window).bind('hashchange', function() {
$('.mydiv').html(window.location.hash);
});
<div class="mydiv"></div>
Related
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
So, i have this html code:
<div class = "form_wrappers">
<div class = "form">Hello
</div>
</div>
<a class = "destiny_add">
And jQuery code:
function addBlock() {
$('.destiny_add').on('click', function (e) {
e.preventDefault();
$('.form_wrappers').append($('form'))
});
}
I want to add a lot of <div class = "form"> in <div class = "form_wrappers>
How can i make it?
Now it tottaly does not work
There are a couple of issues with your code.
Your jquery is inside the function 'addBlock' which is never called.
You're missing the dot in your append $('form')
You need to append the html of the element, not the element itself.
I also included your code in the .ready() to make sure the elements are loaded before jquery tries to run it.
See the code below
$(function() {
$('.destiny_add').on('click', function (e) {
$('.form_wrappers').append('<div class="form">'+$('.form').html()+'</div>');
});
})
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I wanted to add a class to the parent div depending on particular size of the image inside the parent div.
But this jquery code doesn't works when i add more div element in the parent container
code looks something like this
<div>
<div>
<div>
<img src='http://4.bp.blogspot.com/-Go-gvkEm4Rw/UszzNls6EYI/AAAAAAAAEfQ/qds_K1jXgLE/s1600/doctype-hi-res1-960x305.jpg'/>
</div>
</div>
</div>
I want a jquery code which can add class to the main parent div depending on the size of image of certain width.
Use the image load handler... like
jQuery(function () {
$('img').load(function () {
//check the desired size here
if (this.width > 48) {
$(this).parents(':eq(2)').addClass('special')
}
}).filter(function () {
return this.complete
}).trigger('load')
})
Demo: Fiddle
$(img).parent().addClass('classname');
Yeah I've achieved it already it's similar like you jQuery function only difference is I'm using height instead of width of the image
Here's the jQuery function which I'm using
`
$(window).load(function() {
$('img';).each(function() {
var div = $(this);
var $par = $(this).closest('div');
if(this.height >= 100) {
$par.addClass('';);
} else {
$par.addClass('landscape');
}
})
});
`
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am kinda new to Javascript and Jquery and all I wanted to do is a simple test for a navigation bar. I am trying to figure out why my Jquery doesnt work at all...and when I erase my 2 first lines of Javascript, everything works fine...what the hell??? Someone can explain me why is it like this??
My html :
<div id="navbar">
<ul class="ulbar">
<li id="libar">a
<div id="navbar2"></div>
</li>
</ul>
</div>
My JS and Jquery (on same .js):
var buttonA = document.getElementById("libar");
var buttonB = document.getElementById("navbar2");
$(".libar").click(function(){
$(".navbar2").hide("slow", function() {
alert("test");
});
});
Thank you all
try
$(buttonA).click(function(){
$(".navbar2").hide("slow", function() {
alert("test");
});
});
You are gettin the dom elements BY ID first, and referencing later BY CLASS.
I guess you are not able to differentiate between the "class" and "id" attribute.
// IN JavaScript
var buttonA = document.getElementById("libar").value;
// Using jquery, you shall rewrite it as
var buttonA = $('#libar').val();
So in jquery "id" attribute of html elements are accessed by using "#" where as "class" attributes are access by using ".".
Hope that helps.
try this
var buttonA = document.getElementsByClassName("libar");
var buttonB = document.getElementsByClassName("navbar2");
instead of
var buttonA = document.getElementById("libar");
var buttonB = document.getElementById("navbar2");
I think you have class in your html, not id
OR
You need to add id in html
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone help here for clearing the contents in textbox. I have tried to clear the contents in text box on click of an image using javascript
<input name="newKey" id="newKey" type="text" value="helo" size="38" maxlength="45"/>
<span class="btnClr" id="clear" onclick=clearThis("newKey"></span>
function clearThis(target){
target.value= "";
}
You are clearing a string called 'target' :)
What you want to clear is the DOM element itself:
function clearThis(target) {
target = document.getElementById(target);
target.value = "";
}
Moreover, your onclick attribute needed quoting to not be ambiguous:
onclick='clearThis("search")'
Here is a working fiddle
On another note, consider using less obtrusive JavaScript. It is easier to maintain and develop. Debugging code inside attribute strings can be a real nightmare, and might create portability issues.
Something like:
var clear = document.getElementById("clear");
var search = document.getElementById("search");
function clearThis(element) {
element.value = "";
}
clear.onclick = function(){
clearThis(search);
}
And no JavaScript in your HTML
Here is a fiddle of that
USE GET ELEMENT BY ID.....http://jsfiddle.net/Q7fRB/230/
function clearThis(target){
document.getElementById(target).value= "";
}
working Fiddle
$("#clear").click(function(){
$("#newKey").val('');
});
you can simply do this
function clearThis(target){
document.getElementById(target).value = "";
}