JQuery Tooltip Plugin from Jorn - javascript

I am thinking someone may have run across this one, but not sure. From a high level, I am trying to roll over a input [type=text] and display a tool tip (with the contained value) using the plugin available at http://bassitance.de.
I have to use titles and classes for validation on the specific elements, so I put a blank div to hold the input [type=text] value for the roll over.
Issue:
It won't hold the value of 2 text boxes at once. Once I put a value in the box on the right, the tooltip on the left goes away. Same thing if I switch it aroun. I can't keep a tooltip on more than one element.
Here is the code (Note: You will have to download the plugins in the source as I am not sure where the live versions are if there are any).
<link rel="stylesheet" href="/scripts/jquery-tooltip/jquery.tooltip.css" />
<script type="text/javascript" src="/scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/scripts/jquery-tooltip/jquery.tooltip.min.js"></script>
<script type="text/javascript">
$(function(){
$("input").change(function(){
var newTitle = $(this).val();
$(this).parent().attr("title",newTitle);
// re-init tool tip
reload();
});
// Init tooltip
reload();
});
reload = function(){
$("div").tooltip();
}
</script>
<body>
<table border="1px solid black">
<tr>
<td title="hello">
<div>
<input type="text" value=""/>
</div>
</td>
<td>
<div>
<input type="text" value=""/>
</div>
</td>
</tr>
</table>
<div id="debug"></div>
</body>
</html>

I couldn't understand your question. Do you want to show two tooltips at once or your problem is that the title attribute you set on each reload function erases both titles?
I used this plugin before if you explain more, or show a demo of your example i would be able to help.
Sinan.
EDIT Working version: (It changes titles of 'td's)
$(function(){
$("input").change(function(){
var newTitle = $(this).val();
$(this).parent().parent('td').attr("title",newTitle);
// re-init tool tip
reload();
});
// Init tooltip
reload();
});
reload = function(){
$("[title]").tooltip();
}

Related

WordPress color picker not working on clone jQuery

I want to add a color picker on WordPress. I tried by cloning the <div> and append it, but clone color picker isn't opening.
Here is what I tried:
jQuery(function($) {
$('.select-color').wpColorPicker();
$('.more-select').click(function(e) {
e.preventDefault();
var name = $('.number-container:last').clone();
name.find('.ppom-img-uploader-area').end().appendTo('.ppom-img-uploader-area');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main-container">
<div class="number-container">
<label>name</label>
<input type="text" name="">
<label>color</label>
<input type="text" name="color-name" class="select-color">
</div>
<button class="more-select">more</button>
</div>
Here is also a JSFiddle (as suggested in the comments below). How can I solve this problem?
Thanks in advance.
The color picker is initialized by using this after the DOM is ready: $('.select-color').wpColorPicker();
Any element that did not exist when the DOM was first loaded, will not be affected by any JS that was run at the beginning. After cloning it, you need to initialize the new one.
$('.more-select').click (function (e) {
e.preventDefault();
var name = $('.number-container:last').clone () ;
name.find('.ppom-img-uploader-area').end().appendTo('.ppom-img-uploader-area');
$('.select-color').wpColorPicker();
});
I can't test this for you because the color picker does not work in your snippet (you did not provide the JS for the wpColorPicker() function). I believe it will work in WordPress tho.

Get an element based on a change of another element (jquery)

I have the following code:
<tr class="my-field my-field-color-picker" data-name="background_colour" data-type="color_picker">
<td class="my-input">
<input type="text" class="wp-color-picker">
</td>
</tr>
<tr class="my-field my-field-wysiwyg" data-name="text_block" data-type="wysiwyg">
<td class="my-input">
<iframe>
<html>
<body id="tinymce" class="mce-content-body">
some text
</body>
</html>
</iframe>
</td>
</tr>
I am able to get the text input with class="wp-color-picker" thanks to another thread using $('tr[data-name="background_colour"] input.wp-color-picker')
When this text input has a change of value. I want to change the background color of <body>
<body> is inside an iframe inside a tr element with data-type="wysiwyg" which is on the same level as tr with data-name="background_colour"
It's a bit of a mess so I hope this makes sense.
jsFiddle: https://jsfiddle.net/rk3fcjkb/19/
You can do something like this
$(document).ready(function(){
$('tr[data-name="background_colour"] input.wp-color-picker').on('keyup', function(){
$(this).closest('tr').siblings('tr[data-type="wysiwyg"]').find('iframe').contents().find('body').css({
background: $(this).val()
});
});
});
Demo
PS: Accessing stuff inside iframe can get very nasty.
You need to get the iframe first and then from there go to the body tag:
$( ".wp-color-picker" ).keyup(function() {
$('tr[data-type="wysiwyg"] td iframe').contents().find("body").css("background-color","blue");
});
Fiddle Example
Note: For change to trigger you need to un-focus the input element, if you want it to do it right away change it on keyup instead.

Table Refresh, Every Second

<html>
<body>
<body bgcolor="33FF00">
<script language = "JavaScript">
//-----------------Created Variables Here------------------
timeLeft = 30 //this is counted down from until it hits 0
points = 0 //this is the points system that is added to by 10 each time a duck is clicked
// ----------------Duck Or Sky element-----------------
function duSky(){ //This is a function to tell add points to the points variable if the user clicks a duck.
duckNum = Math.floor((Math.random()*10)+1)
if(duckNum<10){document.write("<img src=images/skyTile.jpg>")}
else{document.write("<img src='images/duckTile.jpg' onClick='duckClick()'")}
}
</script>
<center><img src=images/duckHuntTitle.gif><br> <!Duck Hunt title gif, no background so you can see the background of the page, also centered>
<div name = "tableDiv"> <!Named the table "TableDiv" so that I can refer to it at a later date. This was to try and make my job of refreshing easier>
<table>
<tr>
<td> <script> duSky() </script> </td>
<td> <script> duSky() </script> </td>
<td> <script> duSky() </script> </td>
<td> <script> duSky() </script> </td>
<td> <script> duSky() </script> </td>
</tr> <!Inside of all the table boxes there is a function that designates whether inside will be a duck or sky tile>
<tr> <!This is the duck table that is exactly 1000px wide by 400px height. This is created by 10 200px by 200px boxes, two rows of 5 boxes>
<td> <script> duSky() </script> </td>
<td> <script> duSky() </script> </td>
<td> <script> duSky() </script> </td>
<td> <script> duSky() </script> </td>
<td> <script> duSky() </script> </td>
</tr>
</table>
</div>
<form name="score">
Points <input type="text" name="pointsscored" readonly="readonly"> <!This is the box that is centered that displays the points the player has got, also is now readonly so no tweaking is allowed>
</form>
<form name="timer">
Time <input type="text" name="timeBox" readonly="readonly"> <!This is the timer box that is centered as well that displays how long the player has left and is readonly>
</form>
</center>
<script language = "JavaScript"> //Returns the script to JavaScript to allow for functions to be used that are related to the HTML previous to this
document.timer.timeBox.value = timeLeft //Displays the time left before the game has even started and been clicked so the player immediately knows the time that they have to play with
function timeDecrease(){ //This is the timer function that reduces the timer by a second each time to make the game slowly time out after 30 seconds
setInterval(function(){timeLeft-- //I am still working on the refresh function but hopefully it will be corrected to make the table refresh with every 1000 miliseconds
document.timer.timeBox.value=timeLeft;
//document.tableDiv.reload(true) //trying to get the reload function to work.
},1000); //1000 miliseconds, therefore it is 1 second
}
while(timeLeft < 0){alert("Timeeeeeees Up, you scored: ", points ,"points! well done Duck Slayer!")} //Alert to signify the end of the game.
// ----------------Function for clicking the duck-----------------
function duckClick(){
points = points + 10;
document.score.pointsscored.value = points; //when the player clicks the duck, points will be added to the points box
}
</script>
<center>
<form name = "playButton">
<button type="button" onClick = "timeDecrease()">Play!</button> <!This is the on click function that starts the game/countdown feature>
</center>
</form>
</body>
</html>
I have a problem with getting my graph to work. I am currently working on making a simple point and click game and I need the table to refresh to make the images become randomized in the position. The idea is that it refreshes every second, giving the appearance of true randomization from a previous section of the script. The problem is that I can't get the table to refresh even though I have set a div tag and I am using the reload function. I am hoping that you can help me find a solution to this.
Also the website doesn't recognise the document.tableDiv.reload(true) part but I don't understand how to get the table to refresh with every second that goes past.
P.S if you haven't guessed I am awful at coding but hope to get better.
Use jQuery, it will make your life easier as selectors mean you won't have to repeat so much code.
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
Assign all of the td elements to run duSky() function in a class -- we'll also take the script out of the td as we're going to run it from jQuery's document.ready function:
<td class='dusky'></td>
Next, we make a function to assign each td with the output of duSky():
<script>
// This function takes every table cell with dusky class and inserts output of duSky() into it
$(document).ready(function refresh(){
$('.dusky).each(function(){
this.html = duSky();
})
})
</script>
Finally, we make it refresh every 5 seconds:
<script>
// Make our refresh script run every 5 seconds
setInterval(refresh, 5000);
</script>
Note: I can't test this where I am, code may not be 100% correct, but this is on the right lines. If you change it and it helps, please give me the right code so I can edit the post and give future readers an easier ride.

JavaScript inside jstl iteration

I have the following codes:
<%int number=0;%>
<c:forEach var="row" items="${tAdmin.rows}" varStatus="totalRow" step="1">
<td><%=++number%></td>
<td>
<div id="content" style="table-layout:fixed; width:405px; word-wrap:break-word;">
<script language="JavaScript" type="text/JavaScript">
function load(){
var content='${row.content}';
document.getElementById("content").innerHTML=content;
document.getElementById("content").innerHTML=Utf8.decode(document.getElementById("content").innerHTML);
}
window.onload=load;
</script>
</div>
</td>
</c:forEach>
The problem is that it only shows the result of the last content instead of printing it out line by line according to number.
What you are creating, if you view the page source in the browser, would look something like this (note the ${row.content} will have already been replaced on the server):
<td>0<td>
<td>
<div id="content" style="table-layout:fixed; width:405px; word-wrap:break-word;">
<script language="JavaScript" type="text/JavaScript">
function load(){
var content='The first row content';
document.getElementById("content").innerHTML=content;
document.getElementById("content").innerHTML=
Utf8.decode(document.getElementById("content").innerHTML);
}
window.onload=load;
</script>
</div>
</td>
<td>1<td>
<td>
<div id="content" style="table-layout:fixed; width:405px; word-wrap:break-word;">
<script language="JavaScript" type="text/JavaScript">
function load(){
var content='Some different content';
document.getElementById("content").innerHTML=content;
document.getElementById("content").innerHTML=
Utf8.decode(document.getElementById("content").innerHTML);
}
window.onload=load;
</script>
</div>
</td>
<td>2<td>
<td>
<div id="content" style="table-layout:fixed; width:405px; word-wrap:break-word;">
<script language="JavaScript" type="text/JavaScript">
function load(){
var content='Yet some more content';
document.getElementById("content").innerHTML=content;
document.getElementById("content").innerHTML=
Utf8.decode(document.getElementById("content").innerHTML);
}
window.onload=load;
</script>
</div>
</td>
You're going to have many copies of function load() and many times where window.onload is assigned window.onload=load;
When this arrives at the browser and is interpreted, only the last definition of function load() will be in effect; only the last time you assign window.onload=load; means anything (because you keep replacing the value of window.onload) -- each redefinition of load() will replace the previous one - so only your last var content='${row.content}'; is ever executed.
In addition, you will have many <div> tags with the same id of "content" and that's not allowed.
The content of each of those <td><div>...</div></td> blocks can be set by the JSP/JSTL itself on the server -- there is no need to set the innerHTML via javascript.
You can use the totalRow varStatus that you set up to provide the number for the first <td> -- you don't need to increment your own counter.
You can use Expression Language (EL) to access the content value of each row.
Inline style="blah blah blah" sucks. Use that only if absolutely necessary.
Instead, put all this style in CSS targeting .contentbits:
style="table-layout:fixed; width:405px; word-wrap:break-word;"
becomes
.contentbits {
table-layout:fixed;
width:405px;
word-wrap:break-word;
}
The page fragment becomes much simpler:
<c:forEach var="row" items="${tAdmin.rows}" varStatus="totalRow" step="1">
<td>${totalRow}</td>
<td>
<div class="contentbits">${row.content}</div>
</td>
</c:forEach>
It's not the right way to do it, but a simple solution would be use addEventListener instead onload:
<%int number=0;%>
<c:forEach var="row" items="${tAdmin.rows}" varStatus="totalRow" step="1">
<td><%=++number%></td>
<td>
<div id="content<%=number%>" style="table-layout:fixed; width:405px; word-wrap:break-word;">
<script language="JavaScript" type="text/JavaScript">
window.addEventListener("load", function () {
var element = document.getElementById("content<%=number%>");
element.innerHTML=Utf8.decode('${row.content}');
}, true);
</script>
</div>
</td>
</c:forEach>
In fact your code is using only the last "onload" because, when loading the page, it will execute the javascript load callback only when finish full loading it. So, each time you loop is executed, it updates the load callback reference for the last one, so when onload is triggered, the last only will be executed.
But your code has other errors too. The content id, repeats at the code lot of times, that will make your div getElementById useless, because you have lot of ids that are equal. Ids must be unique to work property.
To finish, it's not a good pattern to mix your HTML with scripts inside, is better to have you logic file (javascript file) outside, then it can make changes in your code when finish to load, reading the html that was generated. You also can create data attribute in your div then read it by the javascript to manage all itens with a specific data attributes.
To keep it simple, I will add an example:
<%int number=0;%>
<c:forEach var="row" items="${tAdmin.rows}" varStatus="totalRow" step="1">
<td><%=++number%></td>
<td>
<div id="content<%=number%>" style="table-layout:fixed; width:405px; word-wrap:break-word;" data-content="${row.content}">
</div>
</td>
</c:forEach>
Now the script file (I'm using jQuery for this example works on any browser):
$(function() {
$("[data-content]").each(function(item) {
$(item).html(Utf8.decode(item.attr('data-content')));
});
});

Copy input value into a div tag

I'm trying to write code that will pull a specific value from an input tag in a javascript function and display it in a separate div tag to be used as a URL. I know it's a little convoluted, but it seems to be the best way I can figure out. Here's the abbreviated code I have so far:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
function init(){
[lots of code]
//JavaScript popup that pulls attributes from a map
var contenta = "<b>Building Type</b>: ${Type}" +
"<br /><b>Hours</b>: ${Hours}" +
"<br /><a target='blank' href=${FloorPlan}>Floor Plan</a>" +
"<br /><input type='text' id='url' value=${BldgName}></input>" +
"<br /><button onclick=revealModal('modalPage') value/>" + button;
var bldginfo = new esri.InfoTemplate("${BldgName}", contenta);
</script>
<div id="modalPage">
<div class="modalBackground">
</div>
<div class="modalContainer">
<div class="modal">
<div class="modalTop">[Close]</div>
<!-- Begin pop-up window edits here-->
<div class="modalBody">
<p>Map goes here. Buttons go to the right.</p>
<script type="text/javascript">
$(":hidden");
</script>
</div>
<!-- End pop-up window edits-->
</div>
</div>
What I'm trying to do is set it so that when the div modalBody loads, it's pulling the input from the input id='URL' in the init function. The input can already read the given value, it just needs to transfer. I'm pretty sure I've screwed up the jQuery somehow, and I'm also not sure that I even need it in the first place. I also don't know if the input tag being in the middle of a function is going to affect anything. I would appreciate any help I can get on this.
Have you looked at the jQuery UI site?
they have a dialog component, that I think you could use and save you a bit of work or at least give you an idea of how you can do it?

Categories

Resources