How to hide element with AngularJS based on DOM element - html

I have an Umbraco setup. And when I edit in the CMS I would like for the "Preview" button to disappear whenever there is not a text-editor present in the DOM.
Angular file in localtion Umbraco/Views/content/edit.html
<form novalidate name="contentForm"
ng-controller="Umbraco.Editors.Content.EditController"
ng-show="loaded"
ng-submit="save()"
val-form-manager>
<umb-panel umb-tabs ng-class="{'editor-breadcrumb': ancestors && ancestors.length > 0}">
<umb-header tabs="content.tabs">
<div class="span7">
<umb-content-name placeholder="#placeholders_entername"
ng-model="content.name" />
</div>
<div class="span5">
<div class="btn-toolbar pull-right umb-btn-toolbar">
<div class="btn-group" ng-animate="'fade'" ng-show="formStatus">
<p class="btn btn-link umb-status-label">{{formStatus}}</p>
</div>
<umb-options-menu ng-show="currentNode"
current-node="currentNode"
current-section="{{currentSection}}">
</umb-options-menu>
</div>
</div>
</umb-header>
<umb-tab-view>
<umb-tab id="tab{{tab.id}}" rel="{{tab.id}}" ng-repeat="tab in content.tabs">
<div class="umb-pane">
<umb-property property="property"
ng-repeat="property in tab.properties">
<umb-editor model="property"></umb-editor>
</umb-property>
<div class="umb-tab-buttons" detect-fold ng-class="{'umb-dimmed': busy}">
<div class="btn-group" ng-show="listViewPath">
<a class="btn" href="#{{listViewPath}}">
<localize key="buttons_returnToList">Return to list</localize>
</a>
</div>
<div class="btn-group" ng-show="!isNew">
<a class="btn" ng-click="preview(content)">
<localize key="buttons_showPage">Preview page</localize>
</a>
</div>
<div class="btn-group dropup" ng-if="defaultButton">
<!-- primary button -->
<a class="btn btn-success" href="#" ng-click="performAction(defaultButton)" prevent-default>
<localize key="{{defaultButton.labelKey}}">{{defaultButton.labelKey}}</localize>
</a>
<a class="btn btn-success dropdown-toggle" data-toggle="dropdown" ng-if="subButtons.length > 0">
<span class="caret"></span>
</a>
Return to list
<!-- sub buttons -->
<ul class="dropdown-menu bottom-up" role="menu" aria-labelledby="dLabel" ng-if="subButtons.length > 0">
<li ng-repeat="btn in subButtons">
<a href="#" ng-click="performAction(btn)" prevent-default>
<localize key="{{btn.labelKey}}">{{btn.labelKey}}</localize>
</a>
</li>
</ul>
</div>
</div>
</div>
</umb-tab>
</umb-tab-view>
<ul class="umb-panel-footer-nav nav nav-pills" ng-if="ancestors && ancestors.length > 0">
<li ng-repeat="ancestor in ancestors">
{{ancestor.name}}
</li>
<li></li>
</ul>
</umb-panel>
</form>
I am not certain this is the correct file - but it seems to be the place where the Preview button is created.
The question now is. Can I somehow determine with angular if the text editor is active and then if it is, show the Preview button next to the Save and publish?

I would suggest the following
1) Determine what properties are accessible on the object that is passed through to the <umb-editor> directive.
Digging into the code I can see things like it has a .view property. You could inspect this at runtime in the browser console to see what additional properties are available
2) You can write a rule on the button to show if the collection of fields contains one with a certain property you might have identified
something like this...
Hope this helps

I found a second solution I might as well add here. I used the Umbraco Plugin Backoffice Tweaking.
Here I added this to my Config\BackofficeTweaking.config file
<?xml version="1.0" encoding="utf-8"?>
<Config>
<Rules>
<Rule Type="HideButtons" Enabled="true" Names="preview" Users="" UserTypes="" ContentTypes="Omraader,Grund,Udstykning,Indstillinger,Niveau,Oversigt_Mappe,Oversigt,Information_Mappe,Information,Afstande_Mappe,Afstande" Description="" />
</Rules>
<Scripts>
<Script Name="example"></Script>
</Scripts>
</Config>
Here you can also make some scripting or do many other things. The plugin also has a visual editor for Umbraco. Pretty easy to use.

Related

How to convert a html page to a complete Vue or React project?

I am currently working on a dashboard based on AdminLTE 3. I removed the parts that I don't need and made some changes on the AdminLTE HTML and CSS files. The thing is I need this project to be a Vue or a React project. For now I only need the index.html (an empty dashboard, navbar, sidebar and a footer). There is one main CSS file which has all the classes that the project uses and there are some js files.
I tried to create an empty Vue project and create components like Navbar.vue, Footer.vue, Sidebar.vue etc. But I don't know how to integrate this one big CSS file for all of these and Js files as well. What should I do? I know there is no way to put all the script tags and links inside every components, that didn't work. Also I couldn't manage to use these files on the App.vue
Is there any way that I can convert this HTML project into a Vue or a React one?
Vue is incrementally adoptable. And very small.
Which means you can use it to render just one button. Or you can render the entire contents of your page with it. Your choice.
Also, you can just take the existing HTML and place it inside the template of a .vue single file component (SFC). It doesn't even need a <script> tag.
Example:
Vue.component('your-question', {
template: `
<div class="post-layout">
<div class="votecell post-layout--left">
<div class="js-voting-container grid fd-column ai-stretch gs4 fc-black-200" data-post-id="64535387">
<button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer fc-theme-primary" data-controller="s-tooltip" data-s-tooltip-placement="right" aria-pressed="false" aria-label="Up vote" data-selected-classes="fc-theme-primary" aria-describedby="--stacks-s-tooltip-9i3a4gec"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button><div id="--stacks-s-tooltip-9i3a4gec" class="s-popover s-popover__tooltip pe-none" aria-hidden="true" role="tooltip">This question shows research effort; it is useful and clear<div class="s-popover--arrow"></div></div>
<div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center c-pointer" itemprop="upvoteCount" data-value="1" role="button" tabindex="0" data-s-tooltip-placement="right" data-controller="null s-tooltip" aria-describedby="--stacks-s-tooltip-lk3p5mtq">0</div><div id="--stacks-s-tooltip-lk3p5mtq" class="s-popover s-popover__tooltip pe-none" aria-hidden="true" role="tooltip">View upvote and downvote totals.<div class="s-popover--arrow"></div></div>
<button class="js-vote-down-btn grid--cell s-btn s-btn__unset c-pointer" data-controller="s-tooltip" data-s-tooltip-placement="right" aria-pressed="true" aria-label="Down vote" data-selected-classes="fc-theme-primary" aria-describedby="--stacks-s-tooltip-ib7zwamw"><svg aria-hidden="true" class="m0 svg-icon iconArrowDownLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 10h32L18 26 2 10z"></path></svg></button><div id="--stacks-s-tooltip-ib7zwamw" class="s-popover s-popover__tooltip pe-none wmx2" aria-hidden="true" role="tooltip" style="margin: 0px;">This question does not show any research effort; it is unclear or not useful (click again to undo)<div class="s-popover--arrow" style=""></div></div>
<button class="js-bookmark-btn s-btn s-btn__unset c-pointer py4 js-gps-track" data-controller="s-tooltip" data-s-tooltip-placement="right" aria-pressed="false" aria-label="Bookmark" data-selected-classes="fc-yellow-600" data-gps-track="post.click({ item: 1, priv: 17, post_type: 1 })" aria-describedby="--stacks-s-tooltip-jn0zc88f">
<svg aria-hidden="true" class="svg-icon iconBookmark" width="18" height="18" viewBox="0 0 18 18"><path d="M6 1a2 2 0 00-2 2v14l5-4 5 4V3a2 2 0 00-2-2H6zm3.9 3.83h2.9l-2.35 1.7.9 2.77L9 7.59l-2.35 1.7.9-2.76-2.35-1.7h2.9L9 2.06l.9 2.77z"></path></svg>
<div class="js-bookmark-count mt4 d-none" data-value=""></div>
</button><div id="--stacks-s-tooltip-jn0zc88f" class="s-popover s-popover__tooltip pe-none" aria-hidden="true" role="tooltip">Bookmark this question.<div class="s-popover--arrow"></div></div>
<a class="js-post-issue grid--cell s-btn s-btn__unset c-pointer py6 mx-auto" href="/posts/64535387/timeline" data-shortcut="T" data-controller="s-tooltip" data-s-tooltip-placement="right" aria-label="Timeline" aria-describedby="--stacks-s-tooltip-ud2iy1dm"><svg aria-hidden="true" class="mln2 mr0 svg-icon iconHistory" width="19" height="18" viewBox="0 0 19 18"><path d="M3 9a8 8 0 113.73 6.77L8.2 14.3A6 6 0 105 9l3.01-.01-4 4-4-4h3L3 9zm7-4h1.01L11 9.36l3.22 2.1-.6.93L10 10V5z"></path></svg></a><div id="--stacks-s-tooltip-ud2iy1dm" class="s-popover s-popover__tooltip pe-none" aria-hidden="true" role="tooltip">Show activity on this post.<div class="s-popover--arrow"></div></div>
</div>
</div>
<div class="postcell post-layout--right">
<div class="s-prose js-post-body" itemprop="text">
<p>I am currently working on a dashboard based on AdminLTE 3. I removed the parts that I don't need and made some changes on the AdminLTE HTML and CSS files. The thing is I need this project to be a Vue or a React project. For now I only need the index.html (an empty dashboard, navbar, sidebar and a footer). There is one main CSS file which has all the classes that the project uses and there are some js files.</p>
<p>I tried to create an empty Vue project and create components like Navbar.vue, Footer.vue, Sidebar.vue etc. But I don't know how to integrate this one big CSS file for all of these and Js files as well. What should I do? I know there is no way to put all the script tags and links inside every components, that didn't work. Also I couldn't manage to use these files on the App.vue</p>
<p>Is there any way that I can convert this HTML project into a Vue or a React one?</p>
</div>
<div class="mt24 mb12">
<div class="post-taglist grid gs4 gsy fd-column">
<div class="grid ps-relative">
<span class="edit-tags-wrapper">htmlreactjsvue.jsdashboardadminlte<span>Edit tags</span></span>
</div>
</div>
</div>
<div class="mb0 ">
<div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4">
<div class="grid--cell mr16" style="flex: 1 1 100px;">
<div class="post-menu">
share<div class="s-popover z-dropdown" style="width: unset; max-width: 28em;" id="se-share-sheet-0"><div class="s-popover--arrow"></div><div><span class="js-title fw-bold">Share a link to this question</span> <span class="js-subtitle">(includes your user id)</span></div><div class="my8"><input type="text" class="js-input s-input wmn3 sm:wmn-initial" readonly=""></div><div class="d-flex jc-space-between mbn4"><button class="js-copy-link-btn s-btn s-btn__link">Copy link</button>CC BY-SA 4.0<div class="js-social-container"></div></div></div>
<span class="lsep">|</span>
edit
<span class="lsep">|</span>
<button id="btnFollowPost-64535387" class="s-btn s-btn__link fc-black-400 h:fc-black-700 pb2 js-follow-post js-follow-question js-gps-track" role="button" data-gps-track="post.click({ item: 14, priv: 17, post_type: 1 })" data-controller="s-tooltip " data-s-tooltip-placement="bottom" data-s-popover-placement="bottom" aria-controls="" aria-describedby="--stacks-s-tooltip-rlrg4nxp">
follow
</button><div id="--stacks-s-tooltip-rlrg4nxp" class="s-popover s-popover__tooltip pe-none" aria-hidden="true" role="tooltip">Follow this question to receive notifications<div class="s-popover--arrow"></div></div>
<span class="lsep">|</span>
close
<span class="lsep">|</span>
flag
<span class="lsep">|</span>
<div data-controller="s-popover" data-s-popover-reference-selector="#btnProtectLoggedIn">
<button id="btnProtectLoggedIn" class="s-btn s-btn__link fc-black-400 h:fc-black-700 px4 pb2 js-gps-track" role="button" aria-controls="divConfirmProtect" data-gps-track="post.click({ item: 12, priv: 17, post_type: 1 })" data-action="s-popover#toggle" data-s-popover-placement="bottom-start" data-s-popover-toggle-class="is-selected" title="disallow answers by anonymous and very new low rep users">
protect
</button>
<div class="s-popover px16" id="divConfirmProtect" role="menu" aria-hidden="true">
<p class="bold mb4">Are you sure?</p>
<p class="mb12">Protect this question if it is highly active and likely to receive spam activity or non-answers (e.g. "Me too!" "Thanks!"). Anonymous and low reputation users can’t answer protected questions. Learn more.</p>
<p class="mb0">
<button id="protect-post-64535387" class="s-btn s-btn__primary mr8" type="button" aria-pressed="false">Protect question</button>
<button class="s-btn s-btn__link" type="button" aria-pressed="false" aria-label="Close" data-action="s-popover#toggle">Cancel</button>
</p>
<div class="s-popover--arrow"></div>
</div>
</div>
</div>
</div>
<div class="post-signature owner grid--cell">
<div class="user-info ">
<div class="user-action-time">
asked <span title="2020-10-26 10:31:30Z" class="relativetime">yesterday</span>
</div>
<div class="user-gravatar32">
<div class="gravatar-wrapper-32"><img src="https://lh3.googleusercontent.com/a-/AOh14Ghue30DHLGWKxu5iK2LpyQskJVNNGvkiWGN2Wt6=k-s32" alt="" width="32" height="32" class="bar-sm"></div>
</div>
<div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
Yasin Demirkaya<span class="d-none" itemprop="name">Yasin Demirkaya</span>
<div class="-flair">
<span class="reputation-score" title="reputation score " dir="ltr">9</span><span title="2 bronze badges" aria-hidden="true"><span class="badge3"></span><span class="badgecount">2</span></span><span class="v-visible-sr">2 bronze badges</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="post-layout--right">
<div id="comments-64535387" class="comments js-comments-container bt bc-black-075 mt12" data-post-id="64535387" data-min-length="15">
<ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="true" data-cansee="false" data-comments-unavailable="false" data-addlink-disabled="false">
</ul>
<div class="comment-form">
<form id="add-comment-64535387" class="" data-placeholdertext="Use comments to ask for more information or suggest improvements. Avoid answering questions in comments."><div class="js-comment-form-layout d-flex fw-wrap jc-end mt8"><div class="w75 fl-grow1"><div class="js-comment-text-input-container"><textarea name="comment" class="s-textarea js-comment-text-input" rows="3" cols="68" placeholder="Use comments to ask for more information or suggest improvements. Avoid answering questions in comments."></textarea></div><span class="text-counter cool">enter at least 15 characters</span> <span class="form-error"></span></div><div class="d-flex fd-column jc-space-between pb4 mb16 ml8"><button type="submit" class="s-btn s-btn__primary">Add Comment</button><button type="button" class="js-edit-comment-cancel s-btn s-btn__link ta-left px2 mt4"></button><br><button type="button" class="js-comment-help-link s-btn s-btn__link ta-left px2">help</button></div></div></form>
</div>
</div>
<div id="comments-link-64535387" style="display: none;">
<a class="js-add-link comments-link" title="Use comments to ask for more information or suggest improvements. Avoid answering questions in comments." href="#" role="button">add a comment</a>
<span class="js-link-separator dno"> | </span>
<a class="js-show-link comments-link dno" title="expand to show all comments on this post" href="#" onclick="" role="button"></a>
</div>
</div>
</div>
`
});
new Vue({
el: '#app'
})
#app {
width: 600px;
}
html, body {
min-width: 0 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Shared/stacks.css?v=3b16a418cc4c">
<link rel="stylesheet" type="text/css" href="https://cdn.sstatic.net/Sites/stackoverflow/primary.css?v=fdd4b4684691">
<div id="app">
<your-question />
</div>
Is it Vue? Entirely.
Does it have any reactive data? Not yet.
How much did it take? A copy-paste, basically.
Obviously, it's more than just that. You'll also need to load the javascript and it can't be in the <template>. And you'll likely want to cut portions of your markup into separate components. But, for starters, all you need to do is present the markup and load your scripts. Everything should work as if it wasn't Vue rendering it.
To make it clearer: Vue can render anything and it doesn't care what it is. It can be an Angular app or a React app or even another Vue app.
A tool like the one you're looking for does not and cannot exist, because it wouldn't know what part of the existing markup should be transformed into a Vue component.
But, basically, each Vue app is one big component, which replaces the DOM element you mount it on.
In the above example this is all the app's markup:
<div id="app">
<your-question />
</div>
And <your-question>, defined as a Vue component does the rendering, when you create a
new Vue({ el: '#app '}).
Also note that, once you create a big Vue component, most IDEs will allow you to select a portion of its template and generate a new Vue component from the selected markup, creating the new SFC for you, copying the template portion into the new file and replacing the removed markup with the new component's tag, also resolving necessary imports and any required methods/computed/data or props.
Since the css file already included all the styles you need, just referencing it in the index.html if you created the project via vue-cli.
For instance, I have a css file named app.css.
Copied it to public folder, where the index.html locates here as well.
public/
- index.html
- app.css
Open index.html, reference as that in HTML. But use <% BASE_URL %> at the beginning.
<link rel="stylesheet" href="<%= BASE_URL %>app.css">
In your case, you don't have to write any css code in vue components. Maybe just copying and pasting the HTML code is working. If you hope to override the style within a specific component, use <style scoped> to avoid impacting global styles.

How to change text and button image (or button) when I click

I am working with angular. When I click a button, I get a dropdown with some choices. How can I change the button image or button and text when I click on it? The goal is for it to return to its original state when it is clicked again.
I leave the image here to understand better.
The button in my case will be an image.
What I want is this: I click the button and open the dropdown, in that dropdown, when I click Start, I want the image of the button to be changed (or the button) and Start to be Pause. If you do the same again, the opposite happens ... that is, if you have Pause, pressing it changes the button and Pause becomes Start.
It is possible? Can anyone help me?
Example, my code
Codepen
HTML
<div class="dropdown">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<img src="">
</button>
<div class="dropdown-menu">
<a class="dropdown-item">Start</a>
<a class="dropdown-item">Add new</a>
</div>
</div>
I am working with angular, if anyone can help me get the solution using their resources, I really appreciate
Use a variable to store the current state and toggle view using it's value.
app.component.ts :
currentState : string = 'pause'
app.component.html :
<div class="dropdown">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<!-- Image is to be shown by default -->
<img src="1.png" *ngIf="currentState=='pause'"">
<!-- Image is to be shown on clicking start -->
<img src="2.png" *ngIf="currentState=='start'"">
</button>
<div class="dropdown-menu">
<a class="dropdown-item" *ngIf="currentState=='pause'" (click)="currentState='start'">Start</a>
<a class="dropdown-item" *ngIf="currentState=='start'" (click)="currentState='pause'">Pause</a>
</div>
</div>
add a function to onChange and do it there,
<div class="dropdown">
<button (change)="changeImg()" type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<img src="{{imgSrc}}">
</button>
<div class="dropdown-menu">
<a class="dropdown-item" *ngIf="isStarted">Start</a>
<a class="dropdown-item" *ngIf="!isStarted">Pause</a>
<a class="dropdown-item">Add new</a>
</div>
</div>
in ts,
isStarted:boolean = false; // default to pause
imgSrc:any;
changeImg(){
// We can even write the simplest code instead the below line.
// isStarted = false ? true : false;
isStarted = !isStarted;
imgSrc = isStarted ? startImageSrc : pauseImageSrc;
}
hope this works, did not test :)

Implementing Custom dropdown in Angular 2/4

I have been working on a custom dropdown functionality with a horizontal divider seperating two sets of values and have general select dropdown functionalities like using up and down arrow to navigate through the values, as well as go to specific values on press of a alphabet.
Code:
<div class="btn-group d-flex dropdown" dropdown>
<div class="floatLabelContainer w-100">
<button id="button-basic" dropdownToggle type="button" float-label [addLabel]="false" [hasFloat]="true" class="mb-2" aria-controls="dropdown-basic">
{{selectedCountry}}
<span class="caret" id="country-caret"></span>
</button>
<label for="button-basic" class="label-class" id="label-class">Country</label>
</div>
<ul id="dropdown-basic" slimScroll width="100%" height="250px" size="3px" alwaysVisible="true" wheelStep="20" *dropdownMenu
class="dropdown-menu d-block" role="menu" aria-labelledby="button-basic">
<li role="menuitem">
<a class="dropdown-item" tabindex="0" (click)="selectedCountry = country.name" *ngFor="let country of restOfCountries | orderBy : 'name'">{{country.name}}</a>
</li>
<li class="divider dropdown-divider"></li>
<li role="menuitem">
<a class="dropdown-item" tabindex="0" (click)="selectedCountry = country.name" *ngFor="let country of asianCountries | orderBy : 'name'">{{country.name}}</a>
</li>
</ul>
</div>
I have been able to achieve the divider part with the above code,
but is there away to implement arrows and alphabet press functionality also.
I would prefer to have angular specific implementation or a plugin, rather than js or jQuery
There is the possibility of evaluating keydown and keyup events:
https://alligator.io/angular/binding-keyup-keydown-events/
According to this you should be able to achieve this functionality by implementing click listeners such as:
<div class="btn-group d-flex dropdown"
(keydown.arrowup)="select(items[i-1])"
(keydown.arrowdown)="select(items[i+1])"
dropdown>
EDIT: so I tried this and turns out this does not work on any item on default. It works on inputs, how the example shows but I could not get i ti to work on a list by now.

Successive bootstrap accordion entries are increasingly large?

I'm using Bootstrap to display a list of people from an SQL server in an accordion (expandable list).
My problem is that there is an increasing amount of whitespace between consecutive entries in the accordion, which I can't get rid of.
Its easiest to see it in action: http://ec2-54-200-151-237.us-west-2.compute.amazonaws.com/#
When the whitespace is clicked the tab above is opened, which is some clue to what is going wrong - however I am quite new to html and javascript.
Here is the javascript that creates the new panel:
function create_panel(first_name, last_name, idx){
var $template = $(".template");
var $newPanel = $template.clone();
$newPanel.find(".collapse").removeClass("in");
$newPanel.find(".accordion-toggle").attr("href", "#" + String(idx))
.text(last_name+" "+first_name);
$newPanel.find(".panel-collapse").attr("id", idx).addClass("collapse").removeClass("in");
return $newPanel;
}
And the html panel group element:
<div class="panel-group" id="accordion1">
<div class="panel panel-default template" style="display:none">
<div class="panel-heading accordion-toggle" data-toggle="collapse" data-parent="#accordion1" href="#collapse20" style="width:100%;">
<a class="panel-title">
Mr. Template <span class="label label-danger" style="float:right">Urgent</span>
</a>
</div>
<div id="collapse20" class="panel-collapse collapse">
<div class="panel-body">
<div class="btn-group">
<button type="button" class="btn"><i class="glyphicon glyphicon-flag"></i></button>
<button type="button" class="btn"><i class="glyphicon glyphicon-earphone"></i></button>
<button type="button" class="btn"><i class="glyphicon glyphicon-ok"></i></button>
</div>
</div>
</div>
</div>
</div>
If anyone can help me see where the problem is, I'd be extremely grateful.
Thanks in advance
I solved this just after posting.
The fact that the extra whitespace was compounding tipped me off:
The javascript was referring to the last created item .template every time it made a new entry - hence the compounding. I added id=template to the panel element and changed the javascript refererence to #template, and the accordion now looks normal.

dropdown multiple options issue

I have a list of contacts, I would like to display the phoneNumbers in inputs with dropdown.
<div id="retrievedContactsDiv" data-bind="foreach: Model.userContacts.contacts()">
<!-- ko if: ($data.phoneNumbers().length >= 1) -->
<div class="control-group span3 offset3 ">
<div class="input-append btn-group">
<input id="appendedInputButton" type="text" data-bind="value:$data.phoneNumbers()[0].phoneNumber()">
<a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#">
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<!-- ko foreach: $data.phoneNumbers() -->
<li><span data-bind="text: $data.phoneNumber()"></span> </li>
<!-- /ko -->
</ul>
</div>
</div>
<!-- /ko -->
</div>
JS
$('.dropdown-menu li span').click(function(){
var elementVal=$(this).text();
$('#appendedInputButton').val('');
$('#appendedInputButton').val(elementVal);
});
My problem is that I can't have same id, if I use class all the phoneNumbers of my contacts list will be modified, How can I do it to display correct value if the user use dropdown ?
http://imageup.fr/uploads/1377867794.jpeg
Use jQuery smart selectors to find the closest common parent and then find the field inside it.
$('.dropdown-menu li span').click(function(){
var elementVal = $(this).text();
$(this).closest('.input-append').find('#appendedInputButton').val(elementVal);
});
Actually this is also wrong (in terms of HTML) because you're stuck with duplicate IDs. I would rather suggest you do the same with a class instead of id, it'll work exactly the same with the suggested JS above.