CSS conditional selector either this or that - css

currently i have something like this
$("#foo").click(function () {callMe();});
$("#bar").click(function () {callMe();});
foo and bar do the same thing. Is there a way for me to define a selector saying either if foo id or bar id is called then trigger the click event ? I tried this but it did not work
$("#foo","#bar").click(function () {callMe();});

you could give them a class and then check the id of the target element:
$('').on('click', function(e) {
if (e.currentTarget.id === 'foo') {
console.log('foo clicked');
} else {
console.log('bar clicked');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="click" id="foo">foo</div>
<div class="click" id="bar">bar</div>
Or if I'm misunderstanding the question and you just want to select both elements using their ids, you can just use
$('#foo,#bar').on('click', function() {});
note the comma is in the quotes, not separate quotes

The syntax and the code looks fine, I think you haven't imported jQuery properly.
I tried the same, and it works fine. Have a look at the code below.
function callMe () {
alert("callMe function called!");
}
$("#foo, #bar").click(function () {callMe();});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="foo"> foo </button>
<button id="bar"> bar </button>

document.getElementById("demo").addEventListener("click", callMe);
function callMe(e) {
let id = e.target.id
console.log(id);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="demo">
<button id="foo">Foo</button>
<br> <br>
<button id="bar">bar</button>
</div>

Related

Use a trigger to evaluate xQuery and use return inside HTML page in eXistDB

I’d like to use a Button/link/whatever to call a xQuery function from my eXist Webapp and use the returned value to display a Bootstrap alert. So include a
<button type="button" class="btn btn-danger btn-circle">execute xQuery</button>
inside my html and if it’s clicked evaluate
let $name := "Peter"
return <div class="alert alert-success" role="alert">Hi {$name}</div>
(actually I want something more complex than this, but you get the idea..)
and return
<div class="alert alert-success" role="alert">Hi Peter</div>
inside my Page. Is this possible and if, how?
Using the Templating System does not help me here and just linking to an xQuery in my Database executes the query but redirects to another page showing the result. Can I use AJAX to evaluate the xQuery and modify the current DOM?
Thanks!
For reasons of completeness and future reference I will post a MWE of this here:
Add a Button and a result panel to a page in eXist:
<a class="btn btn-danger run" id="path/to/the/xquery/in_your_app.xq" href="#">
<i class="glyphicon glyphicon-play" /> Run xQuery
</a>
<img class="load-indicator" src="resources/img/ajax-loader.gif" />
<div style="margin-top: 12pt;" class="output" />
Have a runxquery.js in your apps’ resources folder and reference it in your page.html:
$(document).ready(function() {
$(".run").click(function(ev) {
ev.preventDefault();
file = this.id;
var output = $(this).parent().parent().find(".output");
var indicator = $(this).parent().parent().find(".load-indicator");
function run() {
indicator.show();
output.hide();
$.ajax({
url: "/exist/rest/db/apps/yourapp/" + file,
type: "GET",
success: function(result) {
indicator.hide();
output.html(result);
output.slideDown(600);
}
});
}
if (output.is(":empty")) {
run();
} else {
output.slideUp(800, function() {
output.empty();
run();
});
}
});
});
Clicking the Button will trigger the xquery defined in the id of the button inside your apps folder.
If running the xQuery should return HTML Code, you need to specify this in the xQuery:
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "html";
declare option output:media-type "application/html";
let $text := "This is the alert text"
return
<div class="alert alert-success alert-dismissible" data-dismiss="alert" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"/>
{$text}
</div>

React event handlers not binding properly

I am building a React app with Semantic UI in Meteor. I have had two places where event handlers don't seem to be functioning in any capacity, and I haven't found anything online with a problem to the same extent.
Below is my React class. I have tried various ways of calling the eventHandler methods, but nothing works. That also seems irrelevant since I can't even get an anonymous function to run.
SaveSearchPopout = React.createClass({
getInitialState: function() {
return {username: "", queryname: ""};
},
handleUsernameChange:function(e) {
console.log(e.target.value);
this.setState({username: e.target.value})
},
handleQuerynameChange:function(e) {
this.setState({queryname: e.target.value})
},
handleSave:function(e) {
console.log("handling save");console.log(e);
e.preventDefault();
alert("saving!");
return false;
},
render: function() {
console.log(this);
return (
<div className="ui modal saveSearchPopout">
<div className="header">Save Search</div>
<div className="content">
<form className="ui form" onSubmit={function() {console.log("test");}}>
<div className="field">
<input type="text" name="username"
placeholder="Username"
value={this.state.username}
onChange={function() {console.log("update")}} />
</div>
<div className="field">
<input type="text" name="queryname"
placeholder="Name this search"
value={this.state.queryname}
onChange={this.handleQuerynameChange}></input>
</div>
<div className="actions">
<div className="ui cancel button">Cancel</div>
</div>
<button type="submit">Click</button>
<button className="ui button" type="button"
onClick={function() {console.log("saving");}}>Save</button>
</form>
</div>
</div>
);
}
});
The class is rendered from another classes method which looks like:
saveSearch: function() {
var backingDiv = document.createElement('div');
backingDiv.id = 'shadowPopupBack';
document.getElementsByClassName('content-container')[0].appendChild(backingDiv);
ReactDOM.render(<SaveSearchPopout />, backingDiv);
//this.props.saveSearch;
$('.ui.modal.saveSearchPopout')
.modal({
closeable:false,
onDeny: function() {
var container = document.getElementsByClassName('content-container')[0];
var modalContainer = document.getElementById('shadowPopupBack');
container.removeChild(modalContainer);
}
})
.modal('show');
},
The only button that works is the Semantic UI cancel button.
Has anyone else run into this or have any idea what I am missing. Thanks for the help.
Don't know if this is the case, but in newer versions of React (or JSX), when you pass a function to an HTML component or a custom component, that function is not automatically bound to this instance.
You must bind them manually. For example:
onChange={this.handleQuerynameChange.bind(this)}
Or you could use arrow functions, because they will automatically bind to this:
onChange={e => this.handleQuerynameChange(e)}
I eventually found the answer here. Semantic UI modal component onClose with React
and I haven't worked through it yet, but it looks like this is more Reactive solution than using jQuery to bind eventHandlers: http://www.agilityfeat.com/blog/2015/09/using-react-js-and-semantic-ui-to-create-stylish-apps.
Hope this is helpful to someone else.

Meteor Blaze.renderWithData how to InsertAfter is it possible?

I'm having a problem to insert a template after and not before a node. For example:
//Html looks like this
<div class="questions">
<div class="question"></div>
<div class="question"></div>
<div class="question"></div>
</div>
<template name="question">
<div class="question"></div>
</div>
<template name="questionExtraInfo">
<div class="extra"></div>
</template>
I'm trying to get the following:
<div class="questions">
<div class="question"></div>
<div class="extra"></div>
<div class="question"></div>
<div class="question"></div>
</div>
Calling blaze render inside question event
Template.question.events({
'click .more-details': function () {
var instance = Template.instance();
Blaze.renderWithData(Template.questionExtraInfo, {}, document.querySelector('.questions'), instance.find('.question')));
});
I can only figure out how render it before or inside how about after?
<div class="extra"></div>
<div class="question"></div>
<div class="question"><div class="extra"></div></div>
I think a better approach would be to take advantage of reactivity:
Change your questions template to:
<template name="question">
<div class="question"></div>
{{# if shouldIncludeExtra }}
{{> questionExtraInfo }}
{{/if}}
</template>
The above template should be inside an each loop.
Then in your js something like:
Template.question.helpers({
'shouldIncludeExtra': function() {
// replace 'n' with the actual index. I think `this.index` is
// provided within #each blocks, or you can use the new `#each` helper.
var index = n;
return Session.get('shouldIncludeExtra' + index);
}
});
Then, in your click event, you set a session var based on the index to true:
Template.questions.events({
'click .question': function(e, tpl) {
var question = e.currentTarget;
// You can probably come up with something better here..
var index = $(question).parent().find('> .question').index(question);
Session.set('shouldIncludeExtra' + index, true);
}
});
Because of reactivity, you would see the inserts right away when you fire the click event.
I realize this doesn't really answer the headline of your question, but it should get you the desired outcome.

RactiveJS, Stuck at Tutorial 4. Event proxies

I am following the Tutorials but I am stuck at 4. Event Proxies:
The following doesnt work for me, it just happens nothing:
<button on-click='activate'>Activate!</button>
...
ractive.on( 'activate', function ( event ) {
alert( 'Activating!' );
});
Here is a live example:
http://jsbin.com/kupetofawavu/1/
What am I missing, please?
Put your buttons inside the template and you're good.
Cheers!
Your buttons are outside of the template... move them into the template script tag, and it should work.
The buttons need to be in the template. Change
<script id='template' type='text/ractive'>
<p>Hello, [[name]]!</p>
<p>[[counter]]</p>
<p>[[format(date)]]</p>
</script>
<button id="doit">Count</button>
<button on-click='activate'>Activate!</button>
to
<script id='template' type='text/ractive'>
<p>Hello, [[name]]!</p>
<p>[[counter]]</p>
<p>[[format(date)]]</p>
<button id="doit">Count</button>
<button on-click='activate'>Activate!</button>
</script>

Switch buttons using Jquery

I need a help. I have this structure
<div class="buttons">
<p onclick="function1('param1')">button1</p>
<p onclick="function2('param2')">button2</p>
<p onclick="function3('param3')">button3</p>
</div>
This code works perfecty, but in addition I need add class="active" to current selected button which I can not do. The following code does not work.
$(document).ready(function() {
$('.buttons').click(function(){
if($(this).hasClass('active')){
return false;
}
else{
$('.buttons > p').removeClass('active');
$(this).addClass('active');
}
});
})
Can anyone say how to solve this problem?
You have a typo in
<div class="buutons">
it should be
<div class="buttons">
Also
<p onclick="function3('param3')>button3</p>
should be
<p onclick="function3('param3')">button3</p>
JSFiddle is here.
remove the onclick attribute on the p elements, it will overwrite the listener you registered in your js code, register it in the js code instead.
change you code, add "> p" in your first selector:
$(document).ready(function() {
$('.buttons > p').click(function(){
if($(this).hasClass('active')){
return false;
}
else{
$('.buttons > p').removeClass('active');
$(this).addClass('active');
}
});
})

Resources