I am trying to allow a global function to be called and pass text into an alert box, which is set at the top of the page.
My code is:
<script>
$(document).ready(function () {
$('.fadehelper').fadeIn('slow');
function msg(data, type) {
$('.warning').slideDown("slow").delay(1500).slideUp("slow");
}
});
</script>
And the message box is:
<div class="warning">Help me!</div>
I am not really sure how to do this..
I want to pass a type and a message. The class will be the type, and the message will do where the "Warning" goes.
So if I go somewhere in the page and go msg(warning, "help me!"); I want that to be translated like above.
Can you help? Thank you.
First of all, put an id attribute on the div so that it can be found easily. I will assume you are using alertbox as the id in the following function
Add the following to the end of the msg function:
var box=document.getElementById('alertbox');
box.setAttribute('class',type);
box.innerHTML=data;
It can probably be done shorter in jquery but I don't use jquery
function msg(type, data){
var box=document.getElementById('alertbox');
box.setAttribute('class',type);
box.innerHTML=data;
$('#alertbox').slideDown("slow").delay(1500).slideUp("slow");
}
Got it!
Related
I mean, can I add it later on, like after I closed that div?
If I can't add it in HTML, can I add that in CSS or JS? The thing is that I created website with Wordpress and Elementor.
And I can't edit page as HTML (edit source of html code), but I can add HTML code or CSS code or JS code.
So I want myFunction() to get runned when someone click on div which id is "e-title-2451".
<div id="e-title-2451">click on e-title-2451</div>
<script>
const myDiv = document.querySelector("#e-title-2451")
myDiv.addEventListener('click', myFunction)
function myFunction() {
alert('something happened')
}
</script>
You can try something like:
const myDiv = document.querySelector("#e-title-2451")
myDiv.addEventListener('click', myFunction)
you can use event listeners :
document.querySelector("#e-title-2451").addEventlistener('click',()=>{
//the function code you want to perform
});
for class you can use (".e-titlr-2451")
I am new to Javascript and am trying to make a Javascript .click function. Upon clicking button id #btnTransfer, I want it to check against the listed account id value(s) and throw an error message if it matches a listed value. Here is the code im using. The user's input id #accountid is already printed on the webpage via asp, but is hidden from view. Any help would be greatly appreciated :)
$('#btnTransfer').click(function () {
if ($('#accountid').val() == "123456789") {
popError("Error you cannot preform this action");
}
your code is write https://api.jquery.com/click/ i dont understand what u really want to do explain me more or give us more code
You are missing a closing brace and closing parenthesis. Once those syntax issues are fixed and the reference to jQuery included, it works exactly as you said you wanted it to. I don't know what your popError function is like, so I just have it displaying an alert.
function popError(message) {
alert(message);
}
$('#btnTransfer').click(function () {
if ($('#accountid').val() == "123456789") {
popError("Error you cannot preform this action");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="btnTransfer">Transfer</button>
<input type="hidden" id="accountid" value="123456789" />
I´m having a normal JavaScript-function and want to use the Variable (myVar) also in my jQuery Code - is this possible? and how?:
<a onclick="showtitle(abctitle);" href="#">Testlink</a>
<script>
function showtitle(myVar) {
myTitle = myVar;
}
$(document).ready(function() {
alert(myTitle); //I would like to alert "abctitle"
};
</script>
Firstly, don't mix DOM0 inline event handlers with jQuery. Separate your markup and your logic.
If you use a data- attribute you can put your variable's content in your HTML, and then extract that in the event handler:
<a id="test" data-foo="mytitle" href="#">Testlink</a>
and then:
$(document).ready(function() {
$('#test').on('click', function() {
alert($(this).data('foo'));
}
});
In this code the alert won't appear until the link is actually clicked on, of course.
I believe #Alnitak has a great answer. But if you are just looking to solve the question you asked, wrap abctitle in single quotes and make myTitle a global variable:
<a onclick="showtitle('abctitle 2');" href="#">Testlink</a>
<script>
myTitle = "abctitle";
function showtitle(myVar) {
myTitle = myVar;
}
$(document).ready(function() {
alert(myTitle); //I would like to alert "abctitle"
});
</script>
Also, your document ready function was missing its closing parenthesis )
Working example here: http://jsfiddle.net/CbhxY/
UPDATE
Working example on jsfiddle did not work so well. Try this: http://jsbin.com/ohedab/1/
The JS Bin example also adds the alert call in the showtitle function.
you can do with this
function showtitle(abctitle){
alert(myTitle); //I would like to alert "abctitle"
}
I need to write a user defined function using jQuery to swap two div tags within the page. I created the function but it is not swapping them as desired. In fact, when I move the same code inline it works fine. Is there something I am missing?
It is impossible to debug something that I cannot see, but I wrote a "swapper function" for you:
function swapem($el1, $el2) {
var $t=$el2.clone().insertAfter($el1);
$el1.insertAfter($el2);
$el2.remove();
}
$('#swapper').click(function () {
swapem($('#div1'), $('#div2'));
});
jsFiddle Demo
This function call ResizeableTextbox('myRT'); works well inside javascript(produces a resizeable text box) but doesn't work inside jQuery code.Why?
This produces the resizeable textbox.
<script type="text/javascript">
ResizeableTextbox('myRT');
</script>
But if I give it like this in the jquery code,I dont get the resizeable textbox.
$(".select").change(function () {
$(".select"+increment+" option:selected").each(function ()
{
if($(this).text() =='String')
{
$("<label id=labelid >"+label+"</label>").appendTo(".menu li");
ResizeableTextbox('myRT');//this does not work.How else to code?
}
});
});
Is there any method for function call in jQuery?
Update:
The function call ResizeableTextbox('myRT'); doesn't work anywhere within the jQuery code. It works only inside <script>.
How else to write this function call? some one help me please..
You want to append the resizable textbox to .menu li?
You could do this:
var rt = new ResizeableTextbox('myRT'); //this is for making resizeable text box
$('.menu li').append(rt);
However Im not quite sure if this is what you wan't. Your question is rather vague.
It depends greatly on exactly what 'rt' will contain and how it is then added to the DOM.
If it just contains HTML then obviously $(parentSelector).html(rt) would solve the problem.
If it returns a DOM element then $(rt).appendTo(parentSelector);
If it something else, or is added to the DOM inside your ResizeableTextbox code then I couldn't possibly guess.
Update: If you are using the resizable textbox detailed here: http://www.switchonthecode.com/tutorials/javascript-controls-resizeable-textbox then you would use the following code:
$(document).ready(function() {
var rt = new ResizeableTextbox();
$('#resizable').append(rt.GetContainer());
rt.StartListening();
});